maui-dependency-injection

Register and resolve dependencies in .NET MAUI apps.

Updated Dec 25, 2025
One-click install
npx skills add https://github.com/heldercsousa/MyVocaList --skill maui-dependency-injection-heldercsousa
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: maui-dependency-injection
Source: https://github.com/heldercsousa/MyVocaList/tree/main/.claude/skills/maui-dependency-injection
Command: npx skills add https://github.com/heldercsousa/MyVocaList --skill maui-dependency-injection-heldercsousa

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Guidance for developers to implement robust dependency injection in .NET MAUI applications, covering service registration, lifetimes, constructor injection, automatic resolution via Shell navigation, explicit resolution patterns, platform-specific registrations, and testability best practices.

Core Features & Use Cases

  • Service registration and lifetime guidance (Singleton/Transient/Scoped) across MauiProgram.cs
  • Constructor injection and automatic resolution through DI container
  • Automatic resolution via Shell navigation for registered routes
  • Platform-specific service registrations using preprocessor directives
  • Interface-first pattern to enable easy testing and mocks
  • Clear Gotcha about XAML resource timing and dynamic resolution

Quick Start

Register your services in MauiProgram.cs and inject them via constructors in pages and view models to enable automatic resolution and testability.

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, register services with their lifetimes in MauiProgram.cs and inject them via constructors in pages and view models. This enables automatic resolution and testability across your application.

Can I use Shell navigation to resolve dependencies automatically in MAUI?

Yes, Shell navigation resolves dependencies automatically in MAUI for registered routes. Pages and view models are instantiated with their constructor dependencies injected by the DI container during Shell-based navigation.

What is the best way to register platform-specific services in a MAUI app?

Register platform-specific services in MAUI using preprocessor directives in MauiProgram.cs. This targets individual platform implementations while maintaining interface-first testability patterns for your shared code.

Why does XAML resource parsing fail with dynamic dependency resolution?

XAML resource parsing fails with dynamic dependency resolution due to timing issues during initialization. This known gotcha occurs because XAML resources instantiate before the MAUI dependency container is fully built and ready.

How do I choose the right service lifetime for MAUI dependency injection?

Choose service lifetimes in MAUI dependency injection by evaluating scope needs: Singleton shares one instance app-wide, Transient creates a new instance each time, and Scoped matches a specific context to improve testability and maintainability.

Does the interface-first pattern work for testing MAUI view models?

Yes, the interface-first pattern works for testing MAUI view models by registering interfaces rather than concrete classes. This enables easy mocking and substitution of dependencies during unit tests.