From e3d400c7679179f90dad333464c2f0832250322d Mon Sep 17 00:00:00 2001 From: Chris Pulman Date: Thu, 7 Nov 2024 00:09:24 +0000 Subject: [PATCH] FIx For IViewFor with Generic Class --- README.md | 17 +++++++++++++++++ .../AttributeDefinitions.cs | 12 ++++++++++++ .../IViewFor/IViewForGenerator.Execute.cs | 4 +++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 969ee24..b7c2641 100644 --- a/README.md +++ b/README.md @@ -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")] +public partial class MyReactiveControl : UserControl +{ + public MyReactiveControl() + { + InitializeComponent(); + MyReactiveClass = new MyReactiveClass(); + } +} +``` + +### Usage IViewFor with ViewModel Type + ```csharp using ReactiveUI.SourceGenerators; diff --git a/src/ReactiveUI.SourceGenerators/AttributeDefinitions.cs b/src/ReactiveUI.SourceGenerators/AttributeDefinitions.cs index 09903fa..418bd0c 100644 --- a/src/ReactiveUI.SourceGenerators/AttributeDefinitions.cs +++ b/src/ReactiveUI.SourceGenerators/AttributeDefinitions.cs @@ -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 : Attribute; + +/// +/// IViewForAttribute. +/// +/// +/// +/// Initializes a new instance of the class. +/// +/// Type of the view model. +[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 """; diff --git a/src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.Execute.cs b/src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.Execute.cs index 0422d07..b48a957 100644 --- a/src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.Execute.cs +++ b/src/ReactiveUI.SourceGenerators/IViewFor/IViewForGenerator.Execute.cs @@ -50,9 +50,11 @@ public partial class IViewForGenerator token.ThrowIfCancellationRequested(); + var constructorArgument = attributeData.GetConstructorArguments().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; }