Index of every page on this site: llms.txt
All skills
github avatar

/mvvm-toolkit

@f10fdb8 official
by githubgithub/awesome-copilot42k stars
5,426
Run it yourself

CommunityToolkit.Mvvm (the MVVM Toolkit) core: source generators ([ObservableProperty], [RelayCommand], [NotifyPropertyChangedFor], [NotifyCanExecuteChangedFor], [NotifyDataErrorInfo]), base classes (ObservableObject / ObservableValidator / ObservableRecipient), commands (RelayCommand / AsyncRelayCommand), and validation. Companion skills: mvvm-toolkit-messenger for pub/sub, mvvm-toolkit-di for Microsoft.Extensions.DependencyInjection wiring. Works across WPF, WinUI 3, MAUI, Uno, and Avalonia.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/mvvm-toolkit

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencestroubleshooting.md

≈1.8k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Troubleshooting

Common errors, diagnostics, and gotchas with CommunityToolkit.Mvvm 8.x.


Source-generator diagnostics (MVVMTK0xxx)

The generators emit numbered diagnostics. The most common ones:

Code Meaning Fix
MVVMTK0008 The containing type (or an enclosing type) is not partial Add partial to the class declaration and every enclosing type
MVVMTK0016 [NotifyCanExecuteChangedFor] target is not an accessible IRelayCommand property Make sure the target is a [RelayCommand]-generated command (or a manually declared IRelayCommand property) on the same type
MVVMTK0017 [NotifyDataErrorInfo] used outside ObservableValidator Inherit from ObservableValidator or remove the attribute
MVVMTK0018 [NotifyPropertyChangedRecipients] used outside ObservableRecipient Inherit from ObservableRecipient or remove the attribute
MVVMTK0030 [ObservableProperty] used in a type that does not implement INotifyPropertyChanged (and the class-level [INotifyPropertyChanged] / [ObservableObject] attributes are also missing) Inherit from ObservableObject or apply [INotifyPropertyChanged] / [ObservableObject] to the type
MVVMTK0042 The [ObservableProperty] field belongs to a generic type without proper partial declarations Same fix as MVVMTK0008 (add partial)

Search the full table at: https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/errors/


"Property name collides with field name"

'SampleViewModel' already contains a definition for 'Name'

You named the field with PascalCase:

[ObservableProperty]
private string Name;   // ❌ collides with generated property

Use lowerCamel (or prefixed) instead:

[ObservableProperty]
private string? name;   // ✅ generates Name

"Setter never raises PropertyChanged"

Possible causes:

  1. Same reference assigned. The generator uses EqualityComparer<T>.Default.Equals to detect changes. For reference types where you mutated the same instance, the comparer returns true and notification is skipped. Replace the instance instead of mutating.
  2. Property set to identical value. Same value → no notification by design.
  3. Custom comparer needed. For value types where default equality is wrong, write the property by hand and call SetProperty(ref field, value, comparer).

"ContentDialog throws InvalidOperationException" (WinUI 3)

Not a toolkit issue, but commonly hit from [RelayCommand] async methods. Set XamlRoot before calling ShowAsync(). See the winui3-migration-guide skill for details.


Async [RelayCommand] swallows exceptions

Default behavior: the wrapped task is awaited and the exception is rethrown on the synchronization context. If your method is async void, the generator wraps it as a sync RelayCommand and exceptions become unobserved. Always return Task from [RelayCommand] methods.

If the UI binds to ExecutionTask.Exception to render errors, opt into FlowExceptionsToTaskScheduler = true:

[RelayCommand(FlowExceptionsToTaskScheduler = true)]
private async Task LoadAsync(CancellationToken token) { /* ... */ }

Cancellation appears to do nothing

  • Ensure the wrapped method declares a CancellationToken parameter.
  • Pass the token down to the awaited APIs (HttpClient.GetAsync(url, token), Task.Delay(ms, token), etc.).
  • Catch OperationCanceledException so the UI doesn't see an error.

Messenger handler never fires

Checklist:

  1. The recipient is registered for the exact message type, not a base type. Inheritance is not considered.
  2. The same IMessenger instance is used to send and register (WeakReferenceMessenger.Default vs an injected per-window messenger).
  3. The token (channel) matches between sender and receiver.
  4. With WeakReferenceMessenger, the recipient might already have been garbage-collected. Hold a strong reference somewhere (typically the DI container does this for singleton VMs).
  5. With ObservableRecipient, IsActive must be true — OnActivated is what registers the IRecipient<T> handlers.

OnActivated never runs

ObservableRecipient.OnActivated is invoked when IsActive flips from false to true. If you never set IsActive = true, no handlers register. Common pattern:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    ViewModel.IsActive = true;
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    base.OnNavigatedFrom(e);
    ViewModel.IsActive = false;
}

Memory leak with StrongReferenceMessenger

Strong-ref recipients are pinned until you call Unregister. Either:

  • Inherit from ObservableRecipient (auto-unregisters in OnDeactivated).
  • Switch to WeakReferenceMessenger.Default.
  • Call messenger.UnregisterAll(this) in your dispose / tear-down path.

"Cannot inherit from ObservableValidator and ObservableRecipient"

C# single inheritance — pick one. If you need both:

  • Inherit from ObservableRecipient (or ObservableValidator).
  • Inject IMessenger (or implement validation) on the side via composition.

Or use the class-level [INotifyPropertyChanged] / [ObservableObject] attribute on a custom base type that wraps both pieces.


DI container can't construct ViewModel

Symptom: InvalidOperationException mentioning "Unable to resolve service for type 'X' while attempting to activate 'MyViewModel'".

Causes:

  • Constructor parameter type wasn't registered. Add services.AddX(...).
  • Multiple ambiguous constructors — the container picks the longest one whose dependencies are all registered. If two constructors qualify, an exception is thrown. Mark one as the canonical constructor or remove the ambiguity.
  • Scoped service injected into a singleton (in dev mode with scope validation). Either change the lifetime or inject IServiceScopeFactory and resolve from a scope.

XAML cannot resolve namespace

The type 'local:ContactViewModel' was not found.

XAML namespace mappings need the assembly to be referenced and the namespace to match. If the VM lives in a class library, the mapping needs the assembly name:

xmlns:vm="using:MyApp.Shared.ViewModels;assembly=MyApp.Shared"

(WPF syntax differs slightly: xmlns:vm="clr-namespace:...;assembly=...".)


"Design-time data shows nothing"

Design-time XAML editors instantiate the page without your DI container. Either:

  • Provide a parameterless constructor that bootstraps a design-time VM.
  • Use d:DataContext="{d:DesignInstance Type=vm:ContactViewModel, IsDesignTimeCreatable=True}".
  • Use a separate design-time view model class with hard-coded sample data.

More

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning2mo3 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill provides documentation and code templates for using the .NET CommunityToolkit.Mvvm library. It covers MVVM patterns, source generators, and validation. All code snippets follow standard development practices, and all external links point to trusted official Microsoft or Community Toolkit resources. No security issues were detected.

  • Socket2mo

    1 alert: gptAnomaly

  • Snyk2mo

    Risk: LOW · No issues

Signed by skilld at f10fdb8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated 7 months ago
  • csharp
  • mvvm
  • observable-property
  • relay-command
  • wpf
  • winui
  • maui
  • avalonia
  • validation
  • source-generators

README badge

README badge for github/awesome-copilot/mvvm-toolkit

Provides source generators and base classes for the MVVM Toolkit library: `[ObservableProperty]` for properties, `[RelayCommand]` for commands, `ObservableObject` / `ObservableValidator` / `ObservableRecipient` base classes, and validation via `INotifyDataErrorInfo`. Targets WPF, WinUI 3, MAUI, Uno, and Avalonia apps using CommunityToolkit.Mvvm 8.x.

Generated from the current SKILL.md.

Does this work with WPF, WinUI, MAUI, and Avalonia?
Yes. The MVVM Toolkit supports WPF, WinUI 3, MAUI, Uno, and Avalonia across netstandard2.0, netstandard2.1, and net6.0+.
Do I need to declare classes as partial?
Yes. Every class using [ObservableProperty] or [RelayCommand], and every enclosing type if nested, must be declared partial. Omitting it triggers compiler error MVVMTK0008 or MVVMTK0042.
How do I wire up dependent properties and commands that need to update when an observable field changes?
Use [NotifyPropertyChangedFor(nameof(...))] to propagate PropertyChanged to dependent properties, and [NotifyCanExecuteChangedFor(nameof(...))] to notify commands that their CanExecute state may have changed.
What do I use for form validation?
Inherit from ObservableValidator instead of ObservableObject, apply [NotifyDataErrorInfo] and validation attributes like [Required] and [EmailAddress] to observable fields, then call ValidateAllProperties() and check HasErrors.
Are there companion skills for messaging and dependency injection?
Yes. Load mvvm-toolkit-messenger for IMessenger pub/sub patterns, and mvvm-toolkit-di for Microsoft.Extensions.DependencyInjection integration.

Generated from the current SKILL.md. These answers refresh after source changes.