Explain the MAUI app startup and the role of MauiProgram.
Short answer
MAUI uses the .NET Generic Host. MauiProgram.CreateMauiApp() builds a MauiAppBuilder where you register the App class via UseMauiApp (), configure fonts, register services in the DI container, and add handlers or libraries.
Advertisement
AModel answer
MAUI uses the .NET Generic Host. MauiProgram.CreateMauiApp() builds a MauiAppBuilder where you register the App class via UseMauiApp<App>(), configure fonts, register services in the DI container, and add handlers or libraries. The builder produces a MauiApp that the platform-specific entry points (MainActivity, AppDelegate) bootstrap.
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"));
builder.Services.AddSingleton<IDataService, DataService>();
builder.Services.AddTransient<MainPage>();
return builder.Build();
}Real-world example
A team wires up logging, HTTP clients, and view models in MauiProgram so every page resolves its dependencies from the host container.
Likely follow-ups
- How is DI configured at startup? | What calls CreateMauiApp?
Related topics