mvvm-toolkit-di

Wire CommunityToolkit.Mvvm ViewModels into Microsoft.Extensions.DependencyInjection across XAML app platforms.

38.5k|4.9k|Updated Jun 11, 2025
One-click install
npx skills add https://github.com/github/awesome-copilot --skill mvvm-toolkit-di
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: mvvm-toolkit-di
Source: https://github.com/github/awesome-copilot/tree/main/skills/mvvm-toolkit-di
Command: npx skills add https://github.com/github/awesome-copilot --skill mvvm-toolkit-di

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

The MVVM Toolkit ships no DI container, leaving .NET developers to figure out how to compose ViewModels, services, and IMessenger with Microsoft.Extensions.DependencyInjection on their own. This Skill provides the composition root patterns, lifetime guidance, and injection practices needed to wire a XAML app correctly and avoid service-locator anti-patterns.

Core Features & Use Cases

  • Composition Root Setup: Build the service provider once at startup using Host.CreateDefaultBuilder() or a plain ServiceCollection for WPF, WinUI 3, .NET MAUI, Uno, and Avalonia apps.
  • Lifetime & Registration Guidance: Choose Singleton, Transient, or Scoped lifetimes for ViewModels and services, register IMessenger once, and use keyed services (.NET 8+) for multiple implementations.
  • Testing & Legacy Escape Hatches: Inject fakes through constructors for unit tests, and use Ioc.Default only as a last resort for XAML-instantiated objects.
  • Use Case: When starting a new WinUI 3 app, use this Skill to configure the Generic Host, register a singleton ShellViewModel and transient per-page ViewModels, then resolve each page's ViewModel in code-behind without manual construction.

Quick Start

Ask the AI to set up the dependency injection composition root for a new WPF or WinUI app using the MVVM Toolkit with constructor-injected ViewModels.

Frequently Asked Questions about mvvm-toolkit-di

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up dependency injection with CommunityToolkit.Mvvm?

Build a service provider once at startup, preferably with Host.CreateDefaultBuilder(), and register services and ViewModels in ConfigureServices. Inject dependencies through ViewModel constructors rather than resolving them with a service locator.

What service lifetime should ViewModels use in MVVM apps?

Register shell or main-window ViewModels as Singleton and per-page or per-document ViewModels as Transient so each resolution gets a fresh instance. Scoped lifetimes are rarely needed in client apps unless you create explicit IServiceScope instances.

Does the MVVM Toolkit include a DI container?

No, the MVVM Toolkit deliberately ships no DI container and composes with Microsoft.Extensions.DependencyInjection, the same container used by ASP.NET Core and the .NET Generic Host. It only provides Ioc.Default as a legacy escape hatch.

How do I register IMessenger with dependency injection?

Register the messenger once as a singleton, for example services.AddSingleton<IMessenger>(WeakReferenceMessenger.Default), then inject IMessenger into ObservableRecipient ViewModels through their constructors. Per-window messengers can use keyed or scoped registrations.

Why does 'Unable to resolve service for type X' occur?

This error means a constructor dependency was never registered with the container. Register the missing service or ViewModel in ConfigureServices, and enable scope validation via Host.CreateDefaultBuilder() so mistakes fail at startup instead of first use.

When should I use Ioc.Default instead of constructor injection?

Use Ioc.Default only when constructor injection is impossible, such as XAML-instantiated ViewModels for design-time data, ValueConverters, or control templates. Inside ViewModels and services that the container constructs, always prefer constructor injection for testability.