Advertisement
Intermediate .NET MAUI, Data Binding & MVVM

How does the CommunityToolkit.Mvvm reduce MVVM boilerplate?

Short answer

CommunityToolkit.Mvvm provides source generators. [ObservableProperty] on a partial field generates a full property with change notification; [RelayCommand] on a method generates an ICommand. ObservableObject supplies INotifyPropertyChanged.

Save .NET MAUI mock interview
Advertisement
AModel answer
CommunityToolkit.Mvvm provides source generators. [ObservableProperty] on a partial field generates a full property with change notification; [RelayCommand] on a method generates an ICommand. ObservableObject supplies INotifyPropertyChanged. This removes most hand-written property and command code.
public partial class LoginViewModel : ObservableObject
{
    [ObservableProperty] string username;

    [RelayCommand]
    async Task LoginAsync() => await Shell.Current.GoToAsync("//home");
}
Real-world example
A team cuts a 200-line view model to 60 lines by adopting the toolkit's attributes, with fewer bugs from forgotten notifications.
Likely follow-ups
  • What does [RelayCommand] generate? | What is ObservableObject?
Related topics