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

Chapter 3 The Fundamentals of .NET MAUI

return builder.Build();

}

}

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

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

You may have noticed the usage of #if statements. Due to the nature of compiling for multiple platforms in a single project, you will need to write platform-specific code. If, like me, you do not like the #if statement or at would like to keep its usage to a minimum, then fear not: we will be taking a closer look at minimizing it in Chapter 13.

Summary

In this chapter, you have

•\

Walked through the main components of a .NET MAUI

 

application

•\

Earned a tea break

•\

Learned about the start-up process

•\

Learned about the life of a .NET MAUI application

72

 

Chapter 3 The Fundamentals of .NET MAUI

In the next chapter, you will

•\

Learn about the different possibilities you have to

 

architect your applications

•\

Decide on what architecture to use

•\

Walk through a concrete example by creating your

 

ClockWidget

•\

Learn how to further optimize your implementation

 

using NuGet packages

73

CHAPTER 4

An Architecture to Suit You

In this chapter, you will look through some possible architectural patterns that can be used to build .NET MAUI applications. The objective is to provide you with enough detail to help you find the architecture that best fits you. I want to point out that there are no right answers concerning which architecture to choose. The best option is to go with one that you feel will benefit you and your team.

I aim to quash the following myths throughout the course of this chapter:

“You are forced to use XAML.” “You are forced to use MVVM.”

There seems to be a common misconception that Xamarin.Forms and

.NET MAUI are built largely around using only XAML and MVVM. While this is the most common approach taken by developers, it is not forced upon us.

A Measuring Stick

You will build the same control with each of the options to provide a way to compare the differences. The control you will be building is a ClockWidget. The purpose of this control is to do the following:

© Shaun Lawrence 2023

75

S. Lawrence, Introducing .NET MAUI, https://doi.org/10.1007/978-1-4842-9234-1_4

 

Chapter 4 An Architecture to Suit You

•\

Display the current time in your app.

•\

Update the time every minute.

Figure 4-1 shows a very rough layout of the control with the current date and time. You will tidy this up later with the ability to format the date and time information in Chapter 5, but for now let’s just focus on a limited example to highlight the differences in options. Figure 4-1 shows how the ClockWidget will render in your application when you have finished with this chapter.

Figure 4-1.  Sketch of how the ClockWidget control will render

Prerequisites

Before you get started with each of the architectures you will be reviewing in this chapter, you need to do a little bit of background setup to prepare.

You need to add a single new class. This implementation will allow your widgets to schedule an action of work to be performed after a specific period of time. In your scenario of the ClockWidget, you can schedule an update of the UI. Let’s add this Scheduler class into your project.

•\

Right-click the WidgetBoard project.

•\

Select Add Class.

•\

Give it the name of Scheduler.

•\

Click Add.

76