maui-dependency-injection

Regulate dependency wiring in .NET MAUI with Shell navigation and service lifetimes.

Updated Apr 21, 2026
One-click install
npx skills add https://github.com/snailb1007/snail-codex-skills --skill maui-dependency-injection-snailb1007
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: maui-dependency-injection
Source: https://github.com/snailb1007/snail-codex-skills/tree/main/skills/maui-dependency-injection
Command: npx skills add https://github.com/snailb1007/snail-codex-skills --skill maui-dependency-injection-snailb1007

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Provides guidance on implementing robust dependency injection in .NET MAUI applications, helping you manage lifetimes, registration patterns, and proper timing of service resolution to improve testability and maintainability.

Core Features & Use Cases

  • Lifetime decisions: Choose between AddSingleton, AddTransient, and understand why AddScoped is discouraged in MAUI due to lack of per-page scopes.
  • Registration patterns: Register Pages, ViewModels, and services coherently, and leverage Shell navigation to enable automatic resolution.
  • Platform considerations & pitfalls: Handle platform-specific registrations safely and avoid unregistered pages causing runtime nulls.
  • Use Case: In a multi-page MAUI app, ensure that Page/ViewModel pairs are transient while shared services remain singletons for stability.

Quick Start

Register your services in MauiProgram.cs and register pages for Shell navigation to enable automatic DI resolution.

Frequently Asked Questions about maui-dependency-injection

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

FAQPage Schema
How do I register Pages and ViewModels for dependency injection in .NET MAUI?

To register Pages and ViewModels for dependency injection in .NET MAUI, configure your services in MauiProgram.cs and register pages for Shell navigation to enable automatic DI resolution. Use Transient lifetimes for Page/ViewModel pairs to ensure fresh instances during navigation.

Why should I avoid AddScoped when managing service lifetimes in MAUI?

You should avoid AddScoped for managing service lifetimes in MAUI because the framework lacks per-page scopes, making scoped lifetimes unreliable. Use AddSingleton for shared services and AddTransient for Page/ViewModel pairs to ensure application stability.

How does Shell navigation resolve dependencies automatically in MAUI?

Shell navigation resolves dependencies automatically in MAUI by leveraging registered Page and ViewModel pairs during the routing process. When a page is navigated to, the DI container injects the required services via constructor injection based on your registration patterns.

What causes null reference exceptions when navigating to a page in MAUI?

Null reference exceptions during MAUI navigation are typically caused by unregistered pages. If a page is not properly registered in the dependency injection container before attempting Shell navigation, the framework cannot resolve the required dependencies and returns nulls at runtime.

How do I handle platform-specific service registrations safely in .NET MAUI?

To handle platform-specific service registrations safely in .NET MAUI, apply proper lifetime decisions and ensure safe timing of service resolution. Register platform-specific services coherently within your DI container to avoid runtime nulls and maintain testability.

When is explicit service resolution needed instead of constructor injection in MAUI?

Explicit service resolution in MAUI is needed when constructor injection is insufficient or the timing of service resolution requires direct access. Use explicit resolution carefully to satisfy specific dependencies while avoiding anti-patterns related to unregistered pages and scoped lifetimes.