left-icon

.NET MAUI Succinctly®
by Alessandro Del Sole

Previous
Chapter

of
A
A
A

CHAPTER 2

Sharing Code Among Platforms

Sharing Code Among Platforms


.NET MAUI allows you to build apps that run on Android, iOS, Mac Catalyst, Tizen, and Windows from a single C# codebase. This is possible because, at its core, MAUI allows the sharing among platforms of all the code for the user interface, and all the code that is not platform-specific. This chapter explains how code sharing works and provides suggestions on how you can build class libraries for MAUI.

Code sharing in .NET MAUI

In the previous chapter you saw how to create a .NET MAUI solution, and you walked through the project system. The structure of a .NET MAUI project makes it very simple to write code once and run it on all the supported platforms at no additional cost.

The main reason for this, and the biggest difference with Xamarin.Forms, is that a .NET MAUI project is based on .NET 8, which is the latest version of the .NET technology.

If you expand the Dependencies node of a .NET MAUI project, you will see that there are four flavors of .NET 8, each targeting a specific platform, but all with the same core set of APIs. Figure 17 shows these dependencies.

Dependencies of a .NET MAUI project

Figure 17: Dependencies of a .NET MAUI project

With a common set of APIs, your project can easily target multiple platforms. At debugging time, MAUI knows how to resolve the appropriate .NET 8 flavor depending on the target device. At deployment time, Visual Studio resolves and packages the appropriate dependencies based on the target platform.

In summary, all the code you write will run on all the supported platforms. You still have the option to add customizations by writing platform-specific code in those cases where you need access to specific system APIs or views. This will be the topic of Chapter 9.

Working with libraries

You can extend the codebase of your .NET MAUI solutions with both NuGet packages and class libraries. In both cases, you must keep in mind that:

·     If the library only contains reusable code, it must be built on .NET 8.

·     If the library also contains views, assets, resources, and platform-specific code, it must be built with support to .NET MAUI.

Visual Studio 2022 provides a specific project template to create reusable class libraries for .NET MAUI, called .NET MAUI Class Library, that you can see back in Figure 5. When you create a .NET MAUI library, either as a stand-alone project or as an addition to a .NET MAUI solution, Visual Studio generates a project structure that is similar to the application template. Figure 18 shows how the structure appears in Solution Explorer.

Structure of a .NET MAUI class library

Figure 18: Structure of a .NET MAUI class library

As you can see, a .NET MAUI class library has the same framework dependencies of the .NET MAUI application project, plus an explicit reference to the .NET 8 general framework. It also provides platform subfolders where you will be able to add code that needs to run only on the selected systems. Do not forget to add a reference to the new library from the MAUI app project.

Tip: Visual Studio 2022 allows you to quickly generate NuGet packages for your libraries. In the project properties, you can enable the Generate NuGet package on build option in the Package tab, providing the necessary metadata. When you build the solution, a NuGet package is also built. This is not limited to .NET MAUI libraries; it is also available to .NET libraries.

Chapter summary

This chapter discussed code sharing in .NET MAUI and explained how code sharing works because of .NET 8. Also, you had a look at reusable class libraries with .NET MAUI as the specific target. Now that you have basic knowledge of how code sharing works and why it is fundamental, in the next chapters you will start writing code and building cross-platform user interfaces.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.