Generates ViewBase classes for UWP/Uno projects. ⭐ Last 10 features: - feat: To steps. 2023-03-10 - feat: Removed ReactiveUI support. 2023-02-06 - feat: Added ViewBaseGenerator_IsGeneric setting. 2022-08-22 - feat: Added ViewBaseGenerator_IsAbstract setting. 2022-08-22 - feat: Added ViewBaseGenerator_AddViewModelDependencyProperty setting. 2022-08-22 - feat: Added ConstructorGenerator.InheritFromViewBase. 2022-07-20 - feat: Lowered dotnet version requirements to run the generator. 2022-07-13 - feat: Added auto-detection BaseClass by platform. 2022-07-08 - feat: Added ability to setup ViewModel in constructor generators. 2022-07-08 - feat: Added ViewBaseGenerator_CreateReactiveUIWhenActivated. 2022-06-28 🐞 Last 10 bug fixes: - fix: Fixed ViewBaseGenerator explicit type bug. 2022-08-22 - fix: Fixed metadata bug. 2022-08-22 - fix: Updated H.Generators.Extensions. 2022-08-22 - fix: Splitted settings for generators. 2022-07-13 - fix: Changed prefix for base class to Controls. 2022-07-08 - fix: Fixed base class bug. 2022-07-08 - fix: Added global prefix to viewBase.BaseClass. 2022-07-08 - fix: Fixed PropertyMetadata bug. 2022-07-08 - fix: Added GenerateViewBase setting. 2022-07-08 - fix: Fixed constructor generation. 2022-07-01
$ dotnet add package ViewBaseGeneratorThe main purpose of the generator is to avoid boilerplate code in the code-behind views files, like this:
public abstract partial class PreviewDropViewBase
: ReactiveUI.Uno.ReactiveUserControl<Ratbuddyssey.ViewModels.PreviewDropViewModel>
{
}
public partial class MainView
{
public MainView()
{
InitializeComponent();
_ = this.WhenActivated(disposables =>
{
DataContext = ViewModel;
});
}
}
At the same time, the generator supports the ability to add your code anywhere through the definition of partial methods for special cases:
public partial class MainView
{
partial void BeforeInitializeComponent();
partial void AfterInitializeComponent();
partial void AfterWhenActivated(CompositeDisposable disposables);
public MainView()
{
BeforeInitializeComponent();
InitializeComponent();
AfterInitializeComponent();
_ = this.WhenActivated(disposables =>
{
DataContext = ViewModel;
if (ViewModel == null)
{
return;
}
AfterWhenActivated(disposables);
});
}
}
I also recommend not deleting .xaml.cs files, but leaving them empty like this:
namespace YourNamespace.Views;
public partial class MainView
{
}
Install-Package ViewBaseGenerator
<PropertyGroup>
<ViewBaseGenerator_Namespace>YourNamespace.Views</ViewBaseGenerator_Namespace>
</PropertyGroup
<ItemGroup Label="View Constructors">
<AdditionalFiles Include="Views\**\*.xaml" ViewBaseGenerator_GenerateConstructor="True" ViewBaseGenerator_SetReactiveUIDataContext="True" />
</ItemGroup>
<ItemGroup Label="ViewBase">
<AdditionalFiles Include="Views\**\*.xaml.cs" ViewBaseGenerator_BaseClass="ReactiveUI.Uno.ReactiveUserControl" ViewBaseGenerator_ViewModelNamespace="YourNamespace.ViewModels" />
</ItemGroup>
Although ReactiveUI is supported, you can also use the generator without it,
just to get rid of the InitializeComponent() constructors. In this case, you need code like:
<PropertyGroup>
<ViewBaseGenerator_Namespace>YourNamespace.Views</ViewBaseGenerator_Namespace>
</PropertyGroup
<ItemGroup Label="View Constructors">
<AdditionalFiles Include="Views\**\*.xaml" ViewBaseGenerator_GenerateConstructor="True" />
</ItemGroup>Uno uses Source Generators and there is currently no way to use the output of one generator in another. Therefore, the solution is somewhat more complicated:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0-android;net6.0-ios;net6.0-macos;net6.0-maccatalyst</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<ViewBaseGenerator_Namespace>YourNamespace.Views</ViewBaseGenerator_Namespace>
</PropertyGroup>
<ItemGroup Label="ViewBase">
<AdditionalFiles Include="..\..\shared\YourNamespace.Shared\Views\**\*.xaml.cs" ViewBaseGenerator_BaseClass="ReactiveUI.Uno.ReactiveUserControl" ViewBaseGenerator_ViewModelNamespace="YourNamespace.ViewModels" Visible="False" />
<AdditionalFiles Remove="..\..\shared\YourNamespace.Shared\Views\Navigation\MainView.xaml.cs" />
<AdditionalFiles Include="..\..\shared\YourNamespace.Shared\Views\Navigation\MainView.xaml.cs" ViewBaseGenerator_BaseClass="ReactiveUI.Uno.ReactivePage" ViewBaseGenerator_ViewModelNamespace="YourNamespace.ViewModels" Visible="False" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ViewBaseGenerator" Version="1.3.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<!-- Your base class library -->
<PackageReference Include="ReactiveUI.Uno" Version="16.2.6" />
</ItemGroup>
<ItemGroup>
<!-- Your core project that contains ViewModels -->
<ProjectReference Include="..\YourNamespace.Core\YourNamespace.Core.csproj" />
</ItemGroup>
</Project>