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

CHAPTER 1

Introduction to .NET MAUI

In this chapter, you will gain an understanding of what exactly .NET MAUI is, how it differs from other frameworks, and what it offers you as a developer wishing to build a cross-platform application that can run on both mobile and desktop environments. I will also cover the reasons why you should consider it for your next project by weighing the possibilities and limitations of the framework as well as the rich array of tooling options.

What is .NET MAUI?

.NET Multi-platform App UI, or .NET MAUI for short, is a cross-­ platform framework that allows developers to build mobile and desktop applications written in C# and XAML. It allows developers to target both mobile (Android and iOS) and desktop (macOS and Windows) platforms

from a single codebase. Figure 1-1 shows the platforms officially supported by .NET MAUI and Microsoft.

© Shaun Lawrence 2023

3

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

 

Chapter 1 Introduction to .NET MAUI

Figure 1-1.  .NET MAUI platform support

.NET MAUI provides a single API that allows developers to write once and run anywhere. When building a .NET MAUI application, you write code that interacts with this single cross-platform API and .NET MAUI provides the bridge between your code and the platform-specific layer.

If you take a look inside the prism in Figure 1-1, you can start to understand the components that .NET MAUI both uses and offers. Figure 1-2 shows how an Android application is compiled.

Figure 1-2.  Interacting with .NET MAUI APIs

Of course, there will be times when you need to directly access a platform feature. .NET MAUI also provides enough flexibility that you can achieve this by interacting directly with the platform-specific APIs:

•\

.NET for Android

•\

.NET for iOS

4

 

Chapter 1 Introduction to .NET MAUI

•\

.NET for macOS

•\

Windows UI Library (WinUI) 3

Figure 1-3 shows how the code bypasses the .NET MAUI APIs and interacts directly with the .NET for Android APIs.

Figure 1-3.  Interacting with platform-specific APIs

Digging a Bit Deeper

There are some extra steps that the tooling will perform under the hood to get your application built and ultimately ready for use on each of the possible platforms.

When building a .NET application, even if it is not using .NET MAUI, you will very likely hear the term BCL, which is short for the base class library. This is the foundation of all .NET applications, and in the same way that .NET MAUI abstracts away the platforms you wish to build for, the BCL abstracts away what that platform implements when your application runs.

To run your application on your desired platform, you need a .NET runtime. For Android, iOS, and macOS, this is the Mono runtime. The Mono runtime provides the ability to run .NET code on many different platforms. For Windows, this is Win32. Each of these runtimes provide the functionality required for the BCL and therefore a consistent working environment across all supported platforms.

5

Chapter 1 Introduction to .NET MAUI

I like to think of the BCL as the contract between what we are compiling against and what we are running that compiled code with.

Figure 1-4 shows all of the layers involved in compiling and running a

.NET MAUI application.

Figure 1-4.  The full breakdown

To continue with the example of building for Android in the previous diagrams and taking note of the diagram in Figure 1-4, the following can be said:

Your code is compiled against .NET MAUI, .NET for Android and the base class library. It then runs on the Mono runtime, which provides a full implementation of the base class library on the Android platform.

Looking at the above statement, you can replace the parts that are platform specific with another platform (e.g., swapping Android for iOS) and the statement will still be true.

Where Did It Come From?

.NET MAUI is the evolution of Xamarin.Forms, which itself has a rich history of providing developers with a great way to build cross-platform applications. Of course, no framework is perfect, and Xamarin.Forms certainly had its limitations. Thankfully the team at Microsoft decided

6