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

CHAPTER 3

The Fundamentals of

.NET MAUI

In this chapter, you will dissect the project you created in Chapter 2 and dive into the details of each key area. The focus is to provide a good

overview of what a .NET MAUI single project looks like, where each of the key components are located, and some common ways of enhancing them.

Project Structure

.NET MAUI provides support for multiple platforms from within a single project. The focus is to allow us as developers to share as much code and as many resources as possible.

You will likely hear the term single project a lot during your time working with .NET MAUI. It is a concept that is new to the .NET world as part of .NET MAUI. Its key feature is that you can build applications for multiple different targets from, you guessed it, a single project. If you have ever built .NET applications that aim to share code, you will have noticed that each application you wanted to build and deploy required its own project. The same was true with Xamarin.Forms in that you would have at least one project with your common code and then one project per platform. The single project now houses both the shared code and the platform-specific bits of code.

© Shaun Lawrence 2023

45

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

 

Chapter 3 The Fundamentals of .NET MAUI

Figure 3-1 shows a comparison between the old separate project approach in Xamarin.Forms and the new .NET MAUI project format.

Figure 3-1.  Comparison of Xamarin.Forms projects to a .NET MAUI project

Let’s inspect the project you created in Chapter 2 so that you can start to get an understanding of how .NET MAUI supports the multiple platforms and how they relate to shared code.

The new project has the following structure:

•\ Platforms/: This folder contains all the platform-­ specific code. Inside this folder is a set of folders, each with a name that relates to the platform that it supports. Thus Platforms/Android supports the Android platform.

•\ Resources/: This folder is where you store all your resources for the application. A resource is typically anything you wish to embed in the application that isn’t strictly code, such as an image, a font file, or even an audio or video file.

In the past, resource management was always a pain point when building cross-platform applications. For example, building an application

46

Chapter 3 The Fundamentals of .NET MAUI

for both Android and iOS with Xamarin.Forms could result in needing four or five different sizes of each image rendered in the application.

•\ MauiProgram.cs: This class is where you initialize your

.NET MAUI application. It makes use of the Generic Host Builder, which is the Microsoft approach to encapsulating the requirements of an application. These requirements include but are not limited to dependency injection, logging, and configuration.

•\ App.xaml.cs: This is the main entry point to the cross-­ platform application. Note this line of code from the MauiProgram.cs file includes our App class:

builder.UseMauiApp<App>();

•\ App.xaml: This file includes common UI resources that can be used throughout the application. I will cover these types of resources in much more detail in Chapters 5 and 8.

•\ MainPage.xaml and MainPage.xaml.cs: These two files combine to make up your application’s first page.

•\ AppShell.xaml and AppShell.xaml.cs: These two files enable you to define how your application will be laid out through the use of the .NET MAUI concept called Shell. I will cover Shell extensively in Chapter 5.

Note that wherever you see a .xaml file, there will typically be an associated .xaml.cs file. This is due to limitations in what XAML can provide; it requires an associated C# file to cover the parts that XAML does not support. I will cover XAML much more extensively in Chapter 5.

It is also worth noting that you do not have to write any XAML. Sure,

.NET MAUI and its predecessor, Xamarin.Forms, have a deep connection to XAML but because the XAML is ultimately compiled down to C#,

47