Windows Community Toolkit with .NET Framework and source generators

CommunityToolkit

The new version of Windows Community Toolkit (8) offers various improvements over the previous versions in terms of reducing dramatically the size of the boilerplate code

E.g. instead of writing


private bool _IsReady;
public bool IsReady 
{
	get => _IsReady;
    set {
    	SetProperty(ref _IsReady, value);
    }
}

We can replace it with this:


[ObservableProperty]
private bool _IsReady;

With .NET framework the source generator does not work unless we manually add it to the .cproj file

After installing CommunityToolkit.MVVM package, edit the .cproj file and add this:


<ItemGroup>
	<Analyzer Include="packages\CommunityToolkit.Mvvm.8.0.0\analyzers\dotnet\roslyn4.0\cs\CommunityToolkit.Mvvm.SourceGenerators.dll"/>
</ItemGroup>

This will add the analyzer that generates the code on the fly

Post a Comment

Previous Post Next Post