Get the App
SLTechnology News&Howtos  ›  Development  › 

How to configure Application Lifecycle events in .NET MAUI applications

Shulou Source: shulou.com Published: 2022-06-01 03:07:42 10月01日 Update

This article mainly introduces how to configure application lifecycle events in .NET MAUI applications, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.

Application lifecycle

Typically, applications have different lifecycles or states. .net MAUI applications have the following four lifecycles (execution status):

Run (Running)

Not running (Not running)

Turn off (Deactivated)

Stop (Stopped)

When the window moves to each state, different events are triggered.

Cross-platform life cycle

Here are some predefined lifecycle events available in cross-platform applications:

Created: occurs when an application has never been in a running state and moves to a running state. Usually, when we start a new window.

Activated: occurs when the window moves from the unfocused state to the focused state (unfocused = behind another window).

Deactivated: occurs when the window moves to an unfocused state.

Stopped: occurs when the window becomes hidden. For example, when we minimize it. In this way, the window may be destroyed.

Resumed: the subsequent event of the event has been stopped, but it is different from the event created.

Destroying: occurs when the window is destroyed and de-allocated.

The following is a mapping diagram that shows how the .NET MAUI Framework will map native events.

How to configure lifecycle events

With .NET MAUI Preview 13, you can easily configure lifecycle events in the MauiProgram class using the MauiAppBuilder and ConfigureLifecycleEvents extension methods. This method is available in the Microsoft.Maui.LifecycleEvents namespace.

For common configurations, see the following code example.

Using Microsoft.Maui.LifecycleEvents;namespace MauiApp1;public static class MauiProgram {public static MauiApp CreateMauiApp () {var builder = MauiApp.CreateBuilder (); builder .UseMauiApp () .ConfigureFonts (fonts = > {fonts.AddFont ("OpenSans-Regular.ttf", "OpenSansRegular") }) .ConfigureLifecycleEvents (app = > {/ / Code...) }); return builder.Build ();}} platform-specific lifecycle events

You can also raise platform-specific events for custom settings. The specific events (available) for each platform are listed below:

Android

Currently, the following 21 Android platform-specific events are available:

OnActivityResult

OnApplicationConfigurationChanged

OnApplicationCreate

OnApplicationCreating

OnApplicationLowMemory

OnApplicationTrimMemory

OnBackPressed

OnConfigurationChanged

OnCreate

OnDestroy

OnNewIntent

OnPause

OnPostCreate

OnPostResume

OnRequestPermissionsResult

OnRestart

OnRestoreInstanceState

OnResume

OnSaveInstanceState

OnStart

OnStop

You can use compiler instructions to call events specific to the Android platform through the AddAndroid () extension method.

See the following code example. Here, we will call the OnBackPressed event to move to the previous target.

Public static MauiApp CreateMauiApp () {var builder = MauiApp.CreateBuilder (); builder .UseMauiApp () .ConfigureFonts (fonts = > {fonts.AddFont ("OpenSans-Regular.ttf", "OpenSansRegular") ) .ConfigureLifecycleEvents (AppLifecycle = > {# if ANDROID AppLifecycle.AddAndroid (android = > android .OnBackPack (activity) = > BackPressed ()); # endif}); return builder.Build ();} static bool BackPressed () {return true;} IOS

Currently, the following 10 iOS platform-specific events are available:

ContinueUserActivity

DidEnterBackground

FinishedLaunching

OnActivated

OnResignActivation

OpenUrl

PerformActionForShortcutItem

WillEnterForeground

WillFinishLaunching

WillTerminate

You can use compiler instructions to call events specific to the iOS platform through the AddiOS () extension method.

See the following code example. Here, we will call the WillEnterForeground event, which will be raised when the application is in focus mode.

Public static MauiApp CreateMauiApp () {var builder = MauiApp.CreateBuilder (); builder.UseMauiApp () builder.ConfigureLifecycleEvents (AppLifecycle = > {# if IOS AppLifecycle.AddiOS (ios = > ios .WillEnterForeground ((app) = > EnteredForeground ()); # endif}); return builder.Build ();} static void EnterForeground () {} Windows

Currently, the following eight Windows platform-specific events are available:

OnActivated

OnClosed

OnLaunched

OnLaunching

OnNativeMessage

OnResumed

OnVisibilityChanged

OnWindowCreated

You can use compiler instructions to invoke events specific to the Windows platform through the AddWindows () extension method.

See the following code example. Here, we will call the OnNativeMessage event to access the application instance and delete the title bar.

Public static MauiApp CreateMauiApp () {var builder = MauiApp.CreateBuilder (); builder.UseMauiApp () builder.ConfigureLifecycleEvents (AppLifecycle = > {# if WINDOWS AppLifecycle .AddWindows (windows = > windows.OnNativeMessage ((app, args) = > {app.ExtendsContentIntoTitleBar = false;}); # endif}); return builder.Build ();}

About the lifecycle event definition location for different platforms: Core > LifecycleEvents

Thank you for reading this article carefully. I hope the article "how to configure Application Lifecycle events in .NET MAUI applications" shared by the editor will be helpful to you. At the same time, I also hope that you will support and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Event cycle life application platform state configuration movement difference code method example article run instruction compiler facet compile never application Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS Shulou Technology MariaDB Shulou Tech Info