Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,23 @@ The class must inherit from a UI Control from any of the following platforms and
- Avalonia (Avalonia)
- Uno (Windows.UI.Xaml).

### Usage IViewFor with ViewModel Name - Generic Types should be used with the fully qualified name, otherwise use nameof(ViewModelTypeName)
```csharp
using ReactiveUI.SourceGenerators;

[IViewFor("MyReactiveGenericClass<int>")]
public partial class MyReactiveControl : UserControl
{
public MyReactiveControl()
{
InitializeComponent();
MyReactiveClass = new MyReactiveClass();
}
}
```

### Usage IViewFor with ViewModel Type

```csharp
using ReactiveUI.SourceGenerators;

Expand Down
12 changes: 12 additions & 0 deletions src/ReactiveUI.SourceGenerators/AttributeDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ namespace ReactiveUI.SourceGenerators;
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
internal sealed class IViewForAttribute<T> : Attribute;

/// <summary>
/// IViewForAttribute.
/// </summary>
/// <seealso cref="System.Attribute" />
/// <remarks>
/// Initializes a new instance of the <see cref="IViewForAttribute"/> class.
/// </remarks>
/// <param name="viewModelType">Type of the view model.</param>
[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.IViewForGenerator", "1.1.0.0")]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
internal sealed class IViewForAttribute(string? viewModelType) : Attribute;
#nullable restore
#pragma warning restore
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ public partial class IViewForGenerator

token.ThrowIfCancellationRequested();

var constructorArgument = attributeData.GetConstructorArguments<string>().FirstOrDefault();
var genericArgument = attributeData.GetGenericType();
token.ThrowIfCancellationRequested();
if (!(genericArgument is string viewModelTypeName && viewModelTypeName.Length > 0))
var viewModelTypeName = string.IsNullOrWhiteSpace(constructorArgument) ? genericArgument : constructorArgument;
if (string.IsNullOrWhiteSpace(viewModelTypeName))
{
return default;
}
Expand Down