maui-dependency-injection

Configure dependency injection for .NET MAUI Pages and ViewModels.

1|Updated May 21, 2026
One-click install
npx skills add https://github.com/1k-off/umbraco-observability-playground --skill maui-dependency-injection-1k-off
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: maui-dependency-injection
Source: https://github.com/1k-off/umbraco-observability-playground/tree/main/.agents/skills/maui-dependency-injection
Command: npx skills add https://github.com/1k-off/umbraco-observability-playground --skill maui-dependency-injection-1k-off

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps you configure dependency injection in .NET MAUI so your Pages and ViewModels reliably receive the services they depend on, with correct lifetimes and predictable Shell navigation behavior.

Core Features & Use Cases

  • Correct DI setup in MauiProgram.cs: Register shared services and UI types using appropriate lifetimes for MAUI.
  • Reliable constructor injection: Ensure Pages and ViewModels get dependencies injected and avoid silent nulls caused by missing registrations.
  • Shell navigation auto-resolution: Enable Shell route navigation to resolve the full dependency graph for routed Pages.
  • Platform-specific registrations: Use conditional compilation to register the right implementations per target platform with safe fallbacks.
  • Testability via interface-first design: Swap implementations in tests without changing production wiring.

Quick Start

Ask the AI to generate a MauiProgram.cs service registration plan and a Page/ViewModel wiring approach for your MAUI app, including which items should be Singleton vs Transient and how to ensure Shell navigation resolves everything.

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 for .NET MAUI Pages and ViewModels?

Register Pages, ViewModels, and services in MauiProgram.cs to configure .NET MAUI dependency injection. This ensures ViewModels receive dependencies via constructor injection and Pages resolve correctly during Shell navigation.

When should I use AddSingleton versus AddTransient for MAUI service registration?

Use AddSingleton for shared state services that persist across the app lifecycle, and AddTransient for lightweight, stateless services like ViewModels. Correct lifetime usage in MauiProgram.cs prevents memory leaks and unexpected state sharing during navigation.

Why does Shell navigation in MAUI result in null dependency injection errors?

Shell navigation throws null dependency errors when routed Pages or ViewModels are not registered in MauiProgram.cs. You must explicitly register every navigable Page type and its dependencies so the DI container can resolve the full graph during route-based navigation.

Can I register platform-specific service implementations for Android, iOS, Mac Catalyst, and Windows in MAUI?

Yes, you can register platform-specific service implementations in MAUI using preprocessor directives in MauiProgram.cs. Conditional compilation targets Android, iOS, Mac Catalyst, and Windows, allowing the DI container to resolve the correct platform implementation with safe fallbacks.

Does dependency injection in .NET MAUI support interface-first design for testing?

Dependency injection in .NET MAUI fully supports interface-first design. By registering interfaces rather than concrete classes in MauiProgram.cs, you can swap implementations during unit testing without modifying the production service wiring.

What is the best way to prevent missing-registration null dependency issues in MAUI?

The best way to prevent missing-registration null dependency issues in MAUI is to establish guardrails in MauiProgram.cs that validate service registration. Ensure all required services, Pages, and ViewModels are explicitly registered before attempting constructor injection or Shell navigation.