In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.