maui-dependency-injection

Centralize dependency injection configuration for .NET MAUI apps.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/Petelids/Sudoku --skill maui-dependency-injection-petelids
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: maui-dependency-injection
Source: https://github.com/Petelids/Sudoku/tree/main/skills/maui-skills/maui-dependency-injection
Command: npx skills add https://github.com/Petelids/Sudoku --skill maui-dependency-injection-petelids

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

MAUI apps often struggle with inconsistent service lifetimes, poor testability, and scattered registrations. This guide provides a clear, end-to-end approach to dependency injection in MAUI, covering service registration, lifetime choices, constructor injection, automatic resolution via Shell navigation, explicit resolution, platform-specific registrations, and test-friendly patterns.

Core Features & Use Cases

  • Service registration in MauiProgram.cs: register services, viewmodels, and pages with appropriate lifetimes.
  • Lifetime guidance: AddSingleton, AddTransient, and AddScoped usage with examples.
  • Constructor Injection: prefer DI through constructors for testable, decoupled code.
  • Automatic resolution via Shell Navigation: DI resolves registered pages and viewmodels via Shell routes.
  • Explicit Resolution: resolve services on-demand via IServiceProvider when needed.
  • Platform-Specific Service Registration: select implementations per platform with preprocessor directives.
  • Testability & Interface-First Pattern: define interfaces to enable mocks in tests; swap implementations without changing production code.
  • Gotchas & Best Practices: note DI timing with XAML resources and late-bound resolution in App.CreateWindow.

Quick Start

Register your services in MauiProgram.cs and wire ViewModels to Pages.

Frequently Asked Questions about maui-dependency-injection

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

FAQPage Schema
How do I configure dependency injection in .NET MAUI?

To configure dependency injection in .NET MAUI, you centralize service registrations in MauiProgram.cs. You define services, ViewModels, and Pages using appropriate lifetimes to ensure decoupled, testable code throughout your application.

How does Shell navigation resolve ViewModels and Pages with DI?

Shell navigation resolves ViewModels and Pages with DI by automatically injecting registered dependencies when routing to a page. This requires properly registering your Pages and ViewModels in MauiProgram.cs before navigating.

When should I use AddSingleton, AddTransient, or AddScoped in MAUI?

Use AddSingleton, AddTransient, or AddScoped in MAUI based on desired service lifetimes. AddSingleton shares one instance app-wide, AddTransient creates a new instance each time, and AddScoped manages instances within a specific scope or request context.

Can I register platform-specific service implementations in a .NET MAUI app?

Yes, you can register platform-specific service implementations in a .NET MAUI app. You use preprocessor directives in MauiProgram.cs to select and register the correct implementation per platform during compilation.

Why does dependency injection fail with XAML resources during MAUI App startup?

Dependency injection fails with XAML resources during MAUI App startup because of DI timing issues. XAML resources initialize before the DI container is fully built, requiring careful timing or late-bound resolution in App.CreateWindow to avoid resolution errors.

Does interface-first design improve testability for .NET MAUI ViewModels?

Interface-first design improves testability for .NET MAUI ViewModels by defining abstractions that enable mocking in unit tests. You can swap implementations without changing production code, ensuring constructor injection remains decoupled and test-friendly.