maui-dependency-injection

Configure dependency injection in .NET MAUI apps with service registration and lifetime selection.

1|Updated Jun 1, 2026
One-click install
npx skills add https://github.com/D1ssolve/craft-agents --skill maui-dependency-injection-d1ssolve
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: maui-dependency-injection
Source: https://github.com/D1ssolve/craft-agents/tree/main/skills/maui-dependency-injection
Command: npx skills add https://github.com/D1ssolve/craft-agents --skill maui-dependency-injection-d1ssolve

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Setting up dependency injection in .NET MAUI apps involves subtle decisions about service lifetimes, registration patterns, and Shell navigation wiring, and mistakes like Singleton ViewModels or unregistered Pages cause silent runtime failures that are hard to diagnose. ## Core Features & Use Cases - Service Registration Patterns: Register services, ViewModels, and Pages in MauiProgram.cs with correct lifetime selection (AddSingleton, AddTransient, AddScoped). - Constructor Injection & Shell Integration: Wire ViewModels into Pages via constructor injection and let Shell navigation auto-resolve registered routes and their dependency graphs. - Platform-Specific & Testable Design: Register platform implementations with #if directives and define interfaces so services can be swapped with fakes in unit tests. - Use Case: When building a .NET MAUI app where a DetailPage needs a DetailViewModel and an IDataService, use this Skill to register everything correctly in MauiProgram.cs, avoid stale Singleton ViewModels, and ensure no dependency silently resolves to null. ## Quick Start Ask the AI to set up dependency injection in your .NET MAUI project by registering your services, ViewModels, and Pages in MauiProgram.cs with the correct lifetimes.

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 services in a .NET MAUI app?

Register services on builder.Services inside MauiProgram.CreateMauiApp() using AddSingleton, AddTransient, or AddScoped. Group registrations by category such as services, HTTP clients, ViewModels, and Pages before calling builder.Build().

Should ViewModels be Singleton or Transient in .NET MAUI?

ViewModels should be registered as Transient so each navigation creates a fresh instance with clean state. Singleton ViewModels keep stale data across navigations, which is a common source of bugs in MAUI apps.

Why is my injected dependency null in a .NET MAUI Page?

Dependencies are silently null when the Page is not registered in builder.Services but appears in Shell XAML via ShellContent. MAUI then creates it with the parameterless constructor, skipping injection entirely without throwing an exception.

Does AddScoped work in .NET MAUI dependency injection?

AddScoped behaves as a Singleton in MAUI unless you manually create an IServiceScope, because MAUI has no built-in request scope like ASP.NET Core. Use AddTransient or AddSingleton instead unless you explicitly manage scopes.

How do I register platform-specific services in .NET MAUI?

Use preprocessor directives like #if ANDROID, #elif IOS, and #elif WINDOWS in MauiProgram.cs to register platform implementations of a shared interface. Always include an #else fallback or cover every target platform to avoid null services at runtime.