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

Chapter 3 The Fundamentals of .NET MAUI

This type of registration feels much better suited to a web application where requests come in and a scope will represent a single request. In the mobile and desktop world, your application typically has a single state and therefore is less likely to need scoped registrations. Currently .NET MAUI does not provide any automatic creations of scopes, but you have the power to create your own using the IServiceScopeFactory interface and ultimately its implementation.

Application Lifecycle

Sadly, no two platforms provide the same set of behaviors or lifecycle events such as when an application is started, backgrounded, or closed. This is where cross-platform frameworks provide us with a solid set

of encapsulated events to cover most scenarios. There are four main application states in a .NET MAUI application.

Application States

These are the application states:

•\ Not running: This means that the application has not been started and is not loaded into memory. This is typically when the application has been installed, the device has been powered on, the application was closed by the user, or the operating system has

terminated the application to free up some resources.

•\ Running: This means that the application is visible and is focused.

•\ Deactivated: This means that the application is no longer focused but may still be visible. On mobile, this could mean that the operating system is showing a

62

Chapter 3 The Fundamentals of .NET MAUI

permission request alert (e.g., an application asking for permission to use the camera) or similar.

•\ Stopped: This means that the application is no longer visible.

You can now see how a .NET MAUI application moves between the above four states and the events that are triggered to an application. Figure 3-3 shows the possible states that a .NET MAUI application can take during its lifetime and how it transitions between those states.

Figure 3-3.  Application state lifecycle chart

Before we dive into the details of each of the events that are fired between the state transitions, I need to give you some background on how they can be accessed and why. In order to access these events, you must access the Window class. It certainly isn’t a common concept to have a window in a mobile application, but you must appreciate that you are dealing with a cross-platform framework and therefore an approach that fits desktop as well as mobile. I see it as follows: a mobile application is a single window application, and a desktop is likely to be multi-window.

63

Chapter 3 The Fundamentals of .NET MAUI

Lifecycle Events

Now on to the events that move an application between states.

•\ Created: This event is raised after the platform window has been created. Note that the window may not be visible yet.

•\ Activated: This event is raised when the window is the focused window.

•\ Deactivated: This event is raised when the window is no longer the focused window. Note that the window may still be visible.

•\ Stopped: This event is raised when the window is no longer visible. The application may resume from this state but it is not guaranteed, so it is recommended that you cancel any long-running processes or anything that may consume resources on the device. Mobile operating systems are much stricter on what can happen in the background.

•\ Resumed: This event is raised when an application resumes from the Stopped state. It is recommended to prepare your application for full use again (e.g., subscribe to events or messages, refresh any visible content).

•\ Destroying: This event is raised when the platform window is being destroyed and removed from memory. It is recommended that you unsubscribe from events or messages.

64

Chapter 3 The Fundamentals of .NET MAUI

Handling Lifecycle Events

By default, a .NET MAUI application won’t give you access to the lifecycle events; this is something you must opt in for. In order to opt in, you must modify your App class.

Open Visual Studio. You need to add a new class to your project and call it StateAwareWindow. Your new class will need to be modified so it looks as follows:

public class StateAwareWindow: Window

{

public StateAwareWindow() : base()

{

}

public StateAwareWindow(Page page) : base(page)

{

}

protected override void OnCreated()

{

// Initialise our application

}

}

Inside of your application, you can override all methods that will be executed when the specific event occurs. Each override method follows the naming of the events, as described previously, with a prefix of On. Therefore, to handle the Activated event, you override the

OnActivated method.

The final step is to make use of the new class, so inside your App.xaml. cs file, add the following:

65