Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Semi-auto properties should be converted to partial properties using [ObservableProperty] when possible, which is recommended (doing so makes the code less verbose and results in more optimized code).
The following sample generates MVVMTK0056:
<PropertyGroup>
<LangVersion>preview</LangVersion>
</PropertyGroup>
using CommunityToolkit.Mvvm.ComponentModel;
namespace MyApp;
public partial class SampleViewModel : ObservableObject
{
public string? Name
{
get;
set => SetProperty(ref field, value);
}
}
You can fix this by converting the property to use [ObservableProperty] instead:
using CommunityToolkit.Mvvm.ComponentModel;
namespace MyApp;
public partial class SampleViewModel : ObservableObject
{
[ObservableProperty]
public partial string? Name { get; set; }
}
There is also a built-in code fixer for this diagnostic, to easily fix all occurrences in a single click.
Additional resources
- You can find more examples in the unit tests.
MVVM Toolkit