What Is .NET MAUI And Should You Use It?
Introduction
.NET MAUI stands for Multi-platform App UI, and it is a framework for building native mobile and desktop applications with a single shared code base.
As the successor to Xamarin Forms, .NET MAUI is the next major step in the evolution of Microsoft’s cross-platform development offering, allowing one to target Android, iOS, macOS, and Windows at the same time, thereby achieving even greater re-usability and development efficiency.
At XAM we design and build high quality application solutions with many different types of technologies including Xamarin, .NET, Azure, React, React Native, Flutter, Swift and soon we can add .NET MAUI to that list.
Architecture
A .NET MAUI app interacts primarily with the UI controls, pages, layouts, handlers, and cross-platform APIs provided by the .NET MAUI framework. The .NET MAUI framework in turn communicates with the platform-specific frameworks that are now incorporated into .NET 6, as part of .NET’s unification journey that has started with .NET 5 (see .NET unification vision). These platform-specific frameworks consist of binding libraries that wrap the native platform APIs into .NET code for iOS, macOS, and Android, while using WinUI 3 on Windows. The execution of the app is handled by the .NET runtime, which uses WinRT on Windows and the Mono runtime for the rest.
The diagram below illustrates the various layers of the architecture:
Key Aspects
The MAUI framework provides the following features and benefits:
Cross-Platform
MAUI is a cross-platform framework, which means that developers can:
- share the same business logic across all platforms
- build a single UI that looks the same on all platforms
- share and manage resources (fonts, images, animations, localisation etc.) in a single place for all platforms
- write automated tests that are independent of any platform
- leverage the vast range of libraries, tools, and resources that are available in the .NET ecosystem
Platform Specific Customisation
Even though MAUI’s main focus is on building cross-platform apps with shared code, it is still possible to fully customise the UI and behaviour for each platform as needed. This is because MAUI makes the full native platform APIs available to developers, and it can be accomplished by either:
- customising specific parts of a cross-platform app, or
- building the entire UI separately for one or more of the supported platforms via the native platform APIs
Native Output
Since MAUI is a framework that sits on top of the native platform APIs, MAUI apps are fully native apps. They look, feel, and behave the same way as native apps and they can access all native platform features.
Single Project
Instead of needing to create separate projects for each platform the way it is required with Xamarin Forms, MAUI provides a multi-targeting system that allows one to have a single project holding both shared code and resources, as well as platform-specific code and resources underneath a Platforms
folder. This fosters increased re-usability and a simpler and more efficient development experience.
The screenshot to the right shows the structure of a brand new MAUI project, as of the Preview 8 release.
Hot Reload
Hot reload allows one to modify the source code while the app is running, without needing to stop and restart the app’s execution. This is something that is already available in Xamarin Forms for XAML files, however MAUI also adds hot reload support for C# files, which, together with the new XAML Live Preview feature in Visual Studio 2022, gives developers a significant productivity boost.
Multiple UI Design Patterns
Out of the box MAUI ships with UI controls that are intended to be used with the MVVM design pattern, however the MAUI architecture introduces a layer of abstraction between these controls and the native rendering of the controls, thereby enabling extensibility and allowing third parties to build custom UI control frameworks with completely different design patterns.
An example of this is the Comet framework that uses the MVU design pattern to write UI code in a more fluent and compact manner.
The diagram below from Microsoft illustrates this architectural change that enables decoupling via interfaces:
Blazor Support
Blazor is a Microsoft framework for writing rich client-side web UIs using HTML, CSS, and C# instead of JavaScript, and with MAUI it is possible to host Blazor content inside native apps on all platforms supported by MAUI.
Blazor pages render locally in a BlazorWebView
control, which does not need the WebAssembly runtime engine because it uses the native web rendering features on each platform, and this dramatically reduces the size and memory usage of the app, compared to other native web hosting technologies such as Electron.
This integration via the BlazorWebView
control allows Blazor developers to port their UIs to mobile and desktop apps in order to leverage platform-specific features through native APIs, thereby creating much more enhanced user experiences.
More information about Blazor support can be found here: Introduction to .NET MAUI Blazor
Open-Source
MAUI is an open-source framework part of the open-source .NET platform, which is supported by Microsoft and has a large community of contributors. The source code can be accessed on Github here.
Should you use .NET MAUI?
Whether you should be using .NET MAUI depends on your specific use case, and a couple of scenarios are discussed below.
Note: MAUI will be only be officially released and ready for production in Q2 2022, and it Microsoft does not recommend upgrading production apps until then.
You are already working with Xamarin Forms
Since MAUI is the successor to Xamarin Forms, offering many enhancements as discussed above, it makes perfect sense to upgrade your app to MAUI, the only question being around the timing of the upgrade.
If you have a completed Xamarin Forms app that is running in production and not requiring large updates, then you may want to wait in order to make the upgrade as easy as possible. While Microsoft will provide upgrade tools and multiple upgrade paths, the process itself is not (yet) trivial, especially in regards to third-party package dependencies that may not yet have .NET 6 / MAUI compatibility. Waiting for all the necessary tools, packages, and information to be readily available will reduce the amount of effort needed to perform the upgrade, and there should be sufficient time for it since Xamarin Forms will receive support until Q2 2023.
If, on the other hand, you have a Xamarin Forms app that is in active development, you may want to upgrade as soon as MAUI becomes production ready in order to reduce the amount of code that would need to be upgraded later, and to benefit from all the enhancements and productivity improvements provided by MAUI.
You are planning to build a mobile app and are already invested in the .NET stack
If your company already has software built on .NET, whether for desktop or web, using MAUI for mobile app development would be a natural choice to:
- re-use common libraries and business logic,
- leverage the .NET knowledge within the team, and
- share UI code, either for an existing WinUI desktop application or by wrapping an existing Blazor-based web app into a MAUI container.
You are planning to build a cross-platform native mobile app but have not decided which technology to use
The three main players in the mobile cross-platform space for native apps are MAUI/Xamarin Forms, Flutter, and React Native.
One of the advantages that Flutter and Reactive Native had over Xamarin Forms was their fast inner-loop development experience allowing code changes to be visible immediately via a hot reload mechanism. The newer versions of Xamarin Forms have also had this capability for XAML files, but MAUI now provides full hot reload support for C# files as well, bringing it on par with Flutter and React Native.
There are many other pros and cons for each of these technologies, but at a high level, their key benefits are:
- React Native allows web developers familiar with the React JavaScript library and related patterns to more easily build mobile apps by leveraging their existing web development knowledge
- Flutter takes a unique approach to cross-platform development by using the Skia graphical engine to render controls on a 2D OpenGL canvas rather than building on top of the native controls on each platform. This provides more flexible and more advanced UI rendering capabilities and a guarantee that the UI is pixel-perfect on each platform.
- MAUI’s key advantage is the mature, sophisticated, and widely available .NET ecosystem that provides great interoperability and code re-usability, high quality and well-supported libraries, powerful tools and IDEs, and a large community.
Depending on what your business needs are, choosing the .NET stack is often a preferred option, especially for enterprise development, because of the stability, ubiquity, and support that it offers.
You are planning to build a native cross-platform desktop app
If you only need to target Windows and macOS (via Mac Catalyst) with a cross-platform desktop app, then using MAUI can be a great choice because unlike many of the available cross-platform desktop frameworks, MAUI produces fully native apps, that have greater performance and OS integration compared to web UIs that are just hosted in a native container.
Unfortunately, MAUI doesn’t have official Linux support at the moment, and while it may be added in the future or be developed as a community project, for now a good alternative would be to use the Uno Platform or Avalonia for native .NET-based desktop apps that also support Linux.