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

Chapter 11 Getting Specific

For further information on using the OnPlatform markup extension and other possible markup extensions that enable the customization of your application, please refer to the Microsoft documentation at

https://learn.microsoft.com/dotnet/maui/xaml/markup-extensions/ consume#onplatform-markup-extension.

There will be times when just overriding values like this is not enough. For the more complex scenarios, you need to consider an architecture that is completely new to .NET MAUI and that is the handler architecture.

Handlers

Handlers are an area where .NET MAUI really shines! If you have come from a Xamarin.Forms background, you will appreciate the pain that custom renderers brought. If you don’t have any Xamarin.Forms

experience, you are very lucky! I won’t dig down too deep into the details of the old approach as this is a book on .NET MAUI and not the past; however, I feel there is value in talking about the old issues and how they have been overcome by the new handler architecture.

In both Xamarin.Forms and .NET MAUI, we predominantly build our user interfaces with abstract controls: controls defined in the Microsoft namespace and not specifically any platform controls. These controls eventually need to be mapped down to the platform-specific layer. In the Xamarin.Forms days, you would have a custom renderer. The renderer would be responsible for knowing about the abstract control and also the platform-specific control and mapping property values and event handlers and such between the two. This is considered a tightly coupled design, meaning that it becomes really quite difficult to enhance the controls and their rendering. If you wanted to override a small amount of behavior,

you would have to implement a full renderer responsible for mapping all properties/events. This was very painful!

358

Chapter 11 Getting Specific

In .NET MAUI, this concept of renderers has been entirely replaced with handlers. This new architecture provides some extra layers between the abstract controls in the .NET MAUI namespace and the underlying platform-specific controls being rendered in our applications. This is considered much more loosely coupled, mainly due to the fact that each control will implement a number of interfaces and it is the handler’s responsibility to interact with the interface rather than the specific control. This has many benefits including the fact that multiple controls can all implement the same interface and ultimately rely on the same single handler. It also provides the ability to define smaller chunks of common functionality and, as you all know, smaller classes and files are much easier to read, follow, and ultimately maintain. Figure 11-4 shows how the abstract Button class in .NET MAUI is mapped to the specific controls on each platform.

Figure 11-4.  The handler architecture in .NET MAUI

If you wish to create a new control that needs to map to platform-­ specific implementations, you should follow the pattern shown in Figure 11-4. For example, if you made your FixedWidgetBoard a control in this manner, you would also create an IFixedWidgetBoard interface

and then a FixedWidgetBoardHandler and then map from the virtual view across to a platform view. You didn’t take this approach in your scenario because there was no benefit. In fact, it would result in more code because

359

Chapter 11 Getting Specific

you would need to map to each platform individually. This concept may sound like it will always cause more effort; however, in the situation of a Button, it makes sense because each platform already has a definition of what a button is and how it behaves.

Quite often as application developers you will be using existing controls rather than building your own controls, so rather than needing to build everything you see in Figure 11-4, you can customize controls through the use of handlers.

Customizing Controls with Mappers

Mappers are key to the handler architecture. They define the actions that will be performed when either a property is changed or a command is sent between cross-platform controls and platform-specific views. This piece of information in itself might not be that helpful, but once you gain an understanding of how to modify these actions or provide new ones, you can start to understand just how powerful this can be. The majority of the

.NET MAUI handlers are in the Microsoft.Maui.Handlers namespace, which makes them relatively easy to discover. There are a few exceptions to that rule, which are defined in their documentation at https://learn. microsoft.com/dotnet/maui/user-interface/handlers/#handler- based-views.

It is important to note that by modifying the mappers for handlers, you will be overriding the behavior for all implementations of the control it handles. You can overcome this by creating a class (e.g. MyButton) that inherits from the control you wish to enhance (e.g. Button) and then having your handler target the new class (MyButton).

360