Build, review, or migrate .NET MAUI applications across Android, iOS, macOS, and Windows with correct cross-platform UI, platform integration, and native packaging assumptions.
Dependency Injection in .NET MAUI
Guidance for configuring dependency injection in .NET MAUI apps — service registration in MauiProgram.cs, lifetime selection (Singleton / Transient / Scoped), constructor injection, Shell navigation auto-resolution, platform-specific registrations, and testability patterns. USE FOR: "dependency injection", "DI setup", "AddSingleton", "AddTransient", "AddScoped", "service registration", "constructor injection", "IServiceProvider", "MauiProgram DI", "register services", "BindingContext injection". DO NOT USE FOR: data binding (use maui-data-binding), Shell route configuration (use maui-shell-navigation), unit-test mocking frameworks (use standard xUnit and NSubstitute patterns).
Workflow
- Identify all services, ViewModels, and Pages that need to participate in dependency injection.
- Choose the correct lifetime for each type —
AddSingletonfor shared services,AddTransientfor Pages and ViewModels. - Register all types in
MauiProgram.CreateMauiApp()onbuilder.Services, grouping by category (services, HTTP, ViewModels, Pages). - Register Pages as Shell routes in
AppShell.xaml.csso Shell navigation auto-resolves the full dependency graph. - Wire each Page to its ViewModel via constructor injection, assigning the ViewModel as
BindingContext. - Add platform-specific registrations with
#ifdirectives, ensuring every target platform is covered or has a fallback. - Verify resolution works by running the app and confirming no
nulldependencies or missing-registration exceptions at runtime.
---
References
Related skills
Diagnoses and fixes .NET MAUI development environment issues.
.NET MAUI app lifecycle guidance — the four app states, cross-platform Window lifecycle events (Created, Activated, Deactivated, Stopped, Resumed, Destroying), platform-specific…