CommunityToolkit.Mvvm (core)
Use this skill when authoring or reviewing ViewModels, properties,
commands, or validation in apps that use CommunityToolkit.Mvvm 8.x.
Companion skills. Load
mvvm-toolkit-messengerforIMessenger
pub/sub patterns. Load
mvvm-toolkit-difor
Microsoft.Extensions.DependencyInjectionintegration.
Quick recap.
[ObservableProperty]on private fields inpartial
classes;
[RelayCommand]on instance methods; inherit from
ObservableObject(orObservableValidatorfor input forms,
ObservableRecipientwhen usingIMessenger).
Package & setup
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.*" />
</ItemGroup>
Targets: netstandard2.0, netstandard2.1, net6.0+. Works on .NET, .NET
Framework, Mono. Source generators ship in the same NuGet — no extra
analyzer reference required.
Namespaces:
using CommunityToolkit.Mvvm.ComponentModel; // ObservableObject, [ObservableProperty]
using CommunityToolkit.Mvvm.Input; // [RelayCommand], RelayCommand, AsyncRelayCommand
Universal rule. Every type that uses
[ObservableProperty]or
[RelayCommand]— and every enclosing type, if nested — must be
declared
partial. Without it, the generators emit
MVVMTK0008/MVVMTK0042.
Source generators cheat sheet
| Attribute | Applied to | Generates |
|-----------|-----------|-----------|
| [ObservableProperty] | private field | Public INotifyPropertyChanged property + OnXxxChanging/OnXxxChanged partial-method hooks |
| [NotifyPropertyChangedFor(nameof(Other))] | observable field | Also raises PropertyChanged for the listed property |
| [NotifyCanExecuteChangedFor(nameof(MyCommand))] | observable field | Calls MyCommand.NotifyCanExecuteChanged() on change |
| [NotifyDataErrorInfo] | observable field on ObservableValidator | Calls ValidateProperty(value) from the setter |
| [NotifyPropertyChangedRecipients] | observable field on ObservableRecipient | Broadcast(old, new) after the change |
| [RelayCommand] | instance method | Lazy RelayCommand / AsyncRelayCommand exposed as IRelayCommand / IAsyncRelayCommand |
| [RelayCommand(CanExecute = nameof(CanX))] | instance method | Wires CanExecute to a method or property |
| [RelayCommand(IncludeCancelCommand = true)] | async method with CancellationToken | Also generates XxxCancelCommand |
| [RelayCommand(AllowConcurrentExecutions = true)] | async method | Allows queued/parallel invocations (default disables while running) |
| [RelayCommand(FlowExceptionsToTaskScheduler = true)] | async method | Surfaces exceptions via ExecutionTask instead of awaiting and rethrowing |
| [property: SomeAttr] | observable field or [RelayCommand] method | Forwards SomeAttr onto the generated property (e.g., [JsonIgnore]) |
Naming. Field name / _name / m_name → Name. Method LoadAsync →
LoadCommand (the Async suffix is stripped; a leading On is also
stripped).