Back to results for “Commands”
.NET MAUI › Data Binding & MVVM

How does the CommunityToolkit.Mvvm reduce MVVM boilerplate?

Intermediate

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.

Common follow-ups: What does [RelayCommand] generate? | What is ObservableObject?