thoemmi / TinyLittleMvvm

A small MVVM library for WPF built on top of MahApps.Metro, supporting .NET Framework >= 4.7.2 and .NET Core >= 3
MIT License
132 stars 22 forks source link

adds source generator for INotifyPropertyChanged #42

Closed thoemmi closed 3 years ago

thoemmi commented 3 years ago

The PR adds a new package, TinyLittleMvvm.Analyzers.

This package provides a new attribute TinyLittleMvvm.AutoNotifyAttribute. When applied to a private field, it will automatically generate a public property, raising the PropertyChanged event in the setter. It is based on the source generator sample AutoNotifyGenerator. However, in this implementation the setter first checks if the new value is different (using EqualityComparer<TField>.Default). Additionally it will call the partial method On{PropertyName}Changed(oldValue, newValue).

What's missing is an analyzer, which warns when you set the field directly instead of through the property.

When consuming the analyzer package in a WPF application with .NET Core SDK < 5.0.2xx, you have to set the IncludePackageReferencesDuringMarkupCompilation property on your csproj:

<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>

See https://github.com/dotnet/wpf/issues/3404 and https://github.com/dotnet/sdk/issues/15395 for details.