Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lawrence_shaun_introducing_net_maui_build_and_deploy_crosspl.pdf
Скачиваний:
46
Добавлен:
26.06.2023
Размер:
5.15 Mб
Скачать

Chapter 3 The Fundamentals of .NET MAUI

Table 3-1.  Cross-Platform Lifecycle Events Mapped to the Platform-­ Specific Events

Event

Android

iOS/MacCatalyst

Windows

 

 

 

 

Created

OnPostCreate

FinishedLaunching

Created

Activated

OnResume

OnActivated

Activated(Code

 

 

 

Activated and

 

 

 

Pointer

 

 

 

Activated)

Deactivated

OnPause

OnResignActivation

Activated

 

 

 

(Deactivated)

Stopped

OnStop

DidEnterBackground

Visibility

 

 

 

Changed

Resumed

OnRestart

WillEnterForeground

Resumed

Destroying

OnDestroy

WillTerminate

Closed

 

 

 

 

This list may not provide too much meaning right now and I wouldn’t worry yourself with needing to know this. The aim here is to provide you with a quick look-up to be able to then research if any lifecycle events are going wrong or possibly not the right fit for your solution.

Platform-Specific Lifecycle Events

There are actually many platform-specific lifecycle events that .NET MAUI does not map to. What .NET MAUI does provide is a set of lifecycle events that map consistently across all platforms. The rest in this section are really specific to each individual platform. I won’t be covering all of the details of each individual event; however, I will cover how to make use of one so that you will know how to make use of an event that better suits your use case.

67

Chapter 3 The Fundamentals of .NET MAUI

When searching for information around a platform-specific event, don’t feel constrained to searching for .NET MAUI-specific

documentation. You have the power to leverage the platform APIs. You should be able to search for information in the context of Android or iOS and the code should be relatively easy to translate into C#.

In order to register for a platform-specific event, you need to make use of the ConfigureLifecycleEvents method on the MauiAppBuilder class. Let’s look at a concrete example for each platform. The code in each of the following examples is largely the same but the duplication has been kept to show the bigger picture. I have highlighted the differences in bold to show the key differences.

Android

To receive a notification for an Android lifecycle event, you call the

ConfigureLifecycleEvents method on the MauiAppBuilder object. You can then make use of the AddAndroid method and specify the events you wish to handle and how you wish to handle them.

using Microsoft.Maui.LifecycleEvents;

namespace WidgetBoard;

public static class MauiProgram

{

public static MauiApp CreateMauiApp()

{

var builder = MauiApp.CreateBuilder(); builder

.UseMauiApp<App>()

.ConfigureLifecycleEvents(events =>

68

Chapter 3 The Fundamentals of .NET MAUI

{

#if ANDROID

events.AddAndroid(lifecycle=> lifecycle.OnStart((activity) => OnStart(activity)));

static void OnStart(Activity activity)

{

// Perform your OnStart logic

}

#endif

});

return builder.Build();

}

}

For more information on the available lifecycle events, I recommend checking out the following documentation pages:

Microsoft: https://learn.microsoft.com/dotnet/maui/ fundamentals/app-lifecycle#android

Android: https://developer.android.com/guide/components/ activities/activity-lifecycle

iOS andMacCatalyst

To receive a notification for an iOS lifecycle event, you call the

ConfigureLifecycleEvents method on the MauiAppBuilder object. You can then make use of the AddiOS method and specify the events you wish to handle and how you wish to handle them.

using Microsoft.Maui.LifecycleEvents;

namespace WidgetBoard;

public static class MauiProgram

69

Chapter 3 The Fundamentals of .NET MAUI

{

public static MauiApp CreateMauiApp()

{

var builder = MauiApp.CreateBuilder(); builder

.UseMauiApp<App>()

.ConfigureLifecycleEvents(events =>

{

#if IOS || MACCATALYST events.AddiOS(lifecycle =>

lifecycle.OnActivated((app) => OnActivated(app)));

static void OnActivated(UIKit.UIApplication application)

{

// Perform your OnActivated logic

}

#endif

});

return builder.Build();

}

}

For more information on the available lifecycle events, I recommend checking out the following documentation pages:

Microsoft: https://learn.microsoft.com/dotnet/maui/ fundamentals/app-lifecycle#ios

iOS: ­https://developer.apple.com/documentation/uikit/app_and_ environment/managing_your_app_s_life_cycle?language=objc

70