Back to results for “MauiProgram”
.NET MAUI › Dependency Injection & Services

How is dependency injection set up in MAUI?

Beginner

Answer

MAUI uses Microsoft.Extensions.DependencyInjection through the Generic Host. Register services on builder.Services in MauiProgram.CreateMauiApp, then constructor-inject them into pages and view models. The container resolves the whole graph when a registered page is created.
builder.Services.AddSingleton<IApiClient, ApiClient>();
builder.Services.AddTransient<MainViewModel>();
builder.Services.AddTransient<MainPage>();
Real-world example Every page receives its view model and services through the constructor, so nothing is newed up manually in the UI layer.

Common follow-ups: Where do you register services? | How does a page get its dependencies?