Xamarin… You have done a great job on Android, iOS and Windows but we will miss you soon. Your replacement is almost finished and with many improvements: .NET MAUI.
For anyone wondering what it is, Xamarin is Microsoft's open source platform focused on mobile apps (iOS, Android and Windows). It has the ability to share up to 90% of code between different platforms and is based on C#.
The main advantage of Xamarin over other platforms is code optimization. That is, while to develop Android applications we must have knowledge of Java and Objective-C for iOS applications, in Xamarin we only need to know C# because it allows us to interoperate with all the differences that exist at the code level of the other languages.
Xamarin has been with us for quite a few years and in 2020 Microsoft announced that with the arrival of .NET 6 and .NET 7 a possible replacement would be coming: .NET MAUI.
.NET MAUI is Microsoft's new cross-platform Framework and will allow the creation of desktop and mobile applications based on C# like its predecessor.
.NET MAUI stands out for its ability to unify APIs from different platforms (macOS, Android and Windows) into a single API. This will allow developers to have a unified and more comfortable experience while providing great control over all the functions of each platform.
Microsoft explains how .NET MAUI works in 3 sections:
How .NET MAUI works (Microsoft)In a .NET MAUI app, you write code that primarily interacts with the .NET MAUI API (1).
.NET MAUI then directly consumes the native platform APIs (3).
In addition, app code may directly exercise platform APIs (2), if required.
This allows .NET MAUI to have a unique framework for development on different types of platforms (mobile and desktop).
And what does it bring?
What are its new features?
Let's get to it.
Microsoft is constantly developing and improving its products and .NET MAUI is no exception. Microsoft recently released a preview in which they talked about the new features of .NET MAUI. Let's take a look at them!
.NET MAUI now supports Tizen .NET. For those who don't know what it is, it is a platform that allows running .NET applications on mobiles, TVs and other devices (mainly from Samsung) and now adds support for iOS, macOS and Android to allow running applications with .NET MAUI.
Here is a demo of .NET MAUI with Tizen from Microsoft:
Cool to run C# applications on TVs, right?😉
Now they bring us a new long-awaited improvement: the folder structure for each platform. Now in the solutions we develop we will have separate folders for each platform (Android, iOS, Windows, etc..).
This will facilitate the development of application compatibility with each operating system since the build tasks will come preconfigured so that whatever is in each folder, will be used only on that platform.
This feature comes from the problem of knowing which files should be used on which platform. For example we may have something that we only want to run on Android and on no other platform. That is why this feature has arrived.
Now in .NET MAUI you will be able to add conventions at the end of the file names to specify on which platform it should run. Simply by adding a suffix to the end of the file name such as iOS, Windows, Android…
Thanks to this new feature introduced by Microsoft, we can segment the target platform with arguments. By simply adding
#if we can condition that part of the code to be executed only on the desired platform.
Microsoft adds that by default the target options are:
Thanks to Microsoft's example, we can see what it would look like in practice:
var services = builder.Services; #if WINDOWS services.AddSingleton<ITrayService, WinUI.TrayService>(); services.AddSingleton<INotificationService, WinUI.NotificationService>(); #elif MACCATALYST services.AddSingleton<ITrayService, MacCatalyst.TrayService>(); services.AddSingleton<INotificationService, MacCatalyst.NotificationService>(); #endif services.AddSingleton<HomeViewModel>(); services.AddSingleton<HomePage>();
Now it will be much easier to create user interfaces thanks to the more than 40 controls and layouts that .NET MAUI brings. It also has the ability to support Blazor components, which will make it even easier to provide a good experience on mobile, desktop and web.
These more than 40 controls are segmented into 3 categories:
- Pages (full screen, window and several designs)
- Layouts (user-interface controls and multiple views)
- Views (sliders, labels and buttons)
📚Here you can take a look at all the controls and layouts available in .NET MAUI: .NET MAUI Controls
Finally, at the user interface level Microsoft adds the following: