Xamarin's Substitute is on the way! Discover .NET MAUI and its New Features
Tags: csharp, xamarin, xamarinforms, dotnet
Published: 2022-05-23 10:31:00

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](https://docs.microsoft.com/en-us/xamarin/)** 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.

![How Xamarin works (Microsoft)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/feu11wh2paecvz43pzco.png)

---

## Welcome to .NET MAUI

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](https://docs.microsoft.com/en-us/dotnet/maui/what-is-maui#how-net-maui-works)** (_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](https://docs.microsoft.com/en-us/dotnet/maui/what-is-maui#how-net-maui-works) in 3 sections:

![1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7ywq6z2wshd8vah9min.png)

> 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.

---

## The new features of .NET MAUI

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!

### Tizen Platform

.NET MAUI now supports **[Tizen .NET](https://devblogs.microsoft.com/dotnet/dotnet-maui-rc-2/#adding-tizen-platform)**. 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:

![Hello MAUI (Microsoft)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/815hhqcnwefvca17nssh.gif)

Cool to run C# applications on TVs, right?😉

---

### Multi-platform folders

Now they bring us a new long-awaited improvement: the [folder structure for each platform](https://devblogs.microsoft.com/dotnet/dotnet-maui-rc-2/#1-platform-folders). 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.

![Platform folders (Microsoft)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1dlwyx6ax4c7kmg6gdf.png)

---

### Multiplatform filename

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…_

![Filename convention (Microsoft)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ogafpeugjq5khof87xw5.png)

---

### Conditional compilation
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:
- WINDOWS
- IOS
- MACCATALYST
- ANDROID
- TIZEN

Thanks to [Microsoft's example](https://devblogs.microsoft.com/dotnet/dotnet-maui-rc-2/#3-conditional-compilation), we can see what it would look like in practice:

```csharp
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>();
```

---

### New layouts and controls (+40)

Now it will be much easier to create user interfaces thanks to the **[more than 40 controls and layouts](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/controls/)** that .NET MAUI brings. It also has the ability to **[support Blazor components](https://devblogs.microsoft.com/dotnet/dotnet-maui-rc-1/#whats-in-the-net-maui-release-candidate)**, 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](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/controls/)**

Finally, at the user interface level Microsoft adds the following:
- [Animation](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/animation/basic)
- [Brushes](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/brushes/) for solid and gradient colors
- [Displaying Pop-ups](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/pop-ups)
- [Graphics](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/graphics/) for making the most of `Microsoft.Maui.Graphics` 
- [Shadows](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/shadow)
- Styling with [XAML](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/styles/xaml) and [CSS](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/styles/css)
- [Theming](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/theming) for light and dark modes
- [Visual States](https://docs.microsoft.com/en-us/dotnet/maui/user-interface/visual-states)