App startup and lifecycle
In the .NET MAUI project, the app startup and lifecycle management are handled in the following two files:
MauiProgram.cs
App.xaml
/App.xaml.cs
For the app startup and configuration, .NET Generic Host is used. When the application starts, a .NET Generic Host object is created to encapsulate an app’s resources and lifetime functionality, such as the following:
- Dependency injection (DI)
- Logging
- Configuration
- App shutdown
This enables apps to be initialized in a single location and provides the ability to configure fonts, services, and third-party libraries.
.NET Generic Host
If you are a Xamarin developer, you may not be familiar with .NET Generic Host. In ASP.NET Core, .NET Generic Host is used to encapsulate the resources in an app. In .NET MAUI, the same pattern is borrowed and used for startup and configuration management.
Let’s examine the app startup code in Listing 2.1 (MauiProgram.cs...