Reader small image

You're reading from  .NET MAUI Cross-Platform Application Development - Second Edition

Product typeBook
Published inMar 2024
Reading LevelBeginner
PublisherPackt
ISBN-139781835080597
Edition2nd Edition
Languages
Right arrow
Author (1)
Roger Ye
Roger Ye
author image
Roger Ye

Roger Ye is an embedded system programmer who has great interest in virtualization, Android, and cross-platform technologies. His professional experience includes working with major companies like Motorola, Emerson, Intel, and EPAM, where he held the position of Engineering Manager. At Motorola and Emerson, he worked on embedded system projects for mobile devices and telecommunication infrastructures. He is now an engineering manager at EPAM, working with a team to deliver digital solutions for diverse clients.
Read more about Roger Ye

Right arrow

Using Platform-Specific Features

In the last six chapters, we developed a fundamental password manager app. So far, all platform-specific features have been managed by .NET MAUI, making the developed features appear platform-agnostic. However, addressing platform-specific features is not always avoidable. In this chapter, we will delve into the implementation of such features.

We will explore how to utilize device features on supported platforms. By applying the knowledge gained in this chapter, we will incorporate fingerprint support and a customized Markdown view into our app.

While this chapter does touch on platform-specific features, its primary focus isn’t platform-specific programming. Delving deeper into platform-specific implementations would require specific programming knowledge for Android, iOS, WinUI, and so on. Given this would require a considerable amount of knowledge, instead of teaching you to write your own plugins or specific UI controls for each...

Technical requirements

To test and debug the source code in this chapter, you need to have Visual Studio 2022 installed on your PC or Mac. Please refer to the Development environment setup section in Chapter 1, Getting Started with .NET MAUI, for the details.

The source code for this chapter is available in the following GitHub repository: https://github.com/PacktPublishing/.NET-MAUI-Cross-Platform-Application-Development-Second-edition/tree/main/2nd/chapter07.

To check out the source code of this chapter, we can use the below command:

$ git clone -b 2nd/chapter07 https://github.com/PacktPublishing/.NET-MAUI-Cross-Platform-Application-Development-Second-edition.git PassXYZ.Vault2

To find out more about the source code in this book, please refer to the section Managing the source code in this book in Chapter 2, Building Our First .NET MAUI App.

Implementing platform-specific code

In the realm of .NET MAUI application development, there are numerous instances where we might need to write platform-specific code. In this section, we will examine the guidelines for implementing such code. Following that, we will explore the two most common scenarios that necessitate writing platform-specific code in this chapter. The first scenario involves accessing platform features that are not readily available in .NET MAUI APIs. The second scenario may require creating custom controls or customizing existing controls.

Before delving into how to access platform APIs, it’s important to distinguish between the terms .NET MAUI API and platform API. In Chapter 1, Getting Started with .NET MAUI, we have the following architecture diagram of a .NET MAUI application (Figure 7.1).

A group of colorful rectangular boxes with text  Description automatically generated

Figure 7.1: .NET MAUI architecture

Since most of the components in the architectural diagram are open-source, I’ve numbered the components...

.NET MAUI/Xamarin plugins

To utilize platform features in a cross-platform manner, we typically create components known as plugins. In .NET MAUI plugins (or Xamarin plugins), we establish cross-platform APIs to access native platform features or services. These plugins empower you to write platform-independent code in your shared project while concurrently harnessing native functionality on each platform (Android, iOS, macOS, and Windows).

Plugins abstract platform-specific code, enabling you to use a standardized API in your shared project for accessing native features. This streamlines development and helps you maintain a neater and more readable codebase that adheres to the Model-View-ViewModel (MVVM) pattern.

Although I have used the term “.NET MAUI plugins,” it’s important to note that it’s not limited purely to .NET MAUI. In fact, it’s possible to develop a plugin that can be used by both .NET MAUI and Xamarin.Forms simultaneously....

Customizing controls

.NET MAUI controls are built on the foundation of native controls. There may be instances where we would like these native controls to exhibit customized behavior to cater to our specific needs. Additionally, there might be situations when we need to create our own controls, particularly when the desired native controls are not readily available in .NET MAUI.

In this section, we will discuss the .NET MAUI implementations of cross-platform controls and use an example to illustrate how to enhance a cross-platform control with new capabilities.

In our application, we might want to present a password entry as a secure note, rather than as a standard password entry on the details page. To improve the user experience, we aim to support Markdown text instead of plain text. As .NET MAUI currently does not provide a Markdown view control, we must create our own. We will use this scenario as an example to demonstrate how to expand the functionality of existing controls...

Summary

In this chapter, we presented guidelines on implementing platform-specific code within a .NET MAUI project. There are numerous instances where platform-specific code implementation may be required. Regardless of whether we aim to create a plugin or a custom control, it is crucial to begin with an interface definition. A pre-defined interface facilitates the seamless separation of cross-platform and native implementations. To implement the interface, we have the option to employ either an abstract class or a partial class, allowing us to distribute the implementation for each platform across individual files.

The introduction of HybridWebView opens the door to integrating or reusing JavaScript libraries. By crafting hybrid solutions, we can harness the advantages of both .NET and the JavaScript ecosystem. HybridWebView enables the development of .NET solutions that incorporate various JavaScript frameworks.

Having completed the current version of the password manager...

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
.NET MAUI Cross-Platform Application Development - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781835080597
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Roger Ye

Roger Ye is an embedded system programmer who has great interest in virtualization, Android, and cross-platform technologies. His professional experience includes working with major companies like Motorola, Emerson, Intel, and EPAM, where he held the position of Engineering Manager. At Motorola and Emerson, he worked on embedded system projects for mobile devices and telecommunication infrastructures. He is now an engineering manager at EPAM, working with a team to deliver digital solutions for diverse clients.
Read more about Roger Ye