.NET MAUI & Xamarin Forms Getting Started 

.NET MAUI & Xamarin Forms Getting Started 

.NET Multi-Platform App UI, also known as .NET MAUI, is an open-source cross-platform framework for creating native apps. The main idea here is to enable implementation of UI Layout and all the app logic in a single code-base. 

Key Benefits: 

  • Write cross-platform apps in XAML and C#, from a single shared code-base in Visual Studio.
  • Share UI layout and design across platforms.
  • Share code, tests, and business logic across platforms.
.NET MAUI


How it works

.NET MAUI provides singular APIs for developers, which access native platform APIs. It can also be imagined as a C# wrapper for multiple native platform APIs, providing a write-once, run-anywhere experience.  

Main Idea:

  1. Write code that interacts with .NET MAUI API.
  2. App code can directly access .NET wrapped versions of the native APIs.
  3. .NET MAUI can directly consume the native platform APIs.
.NET MAUI

Android:

.NET MAUI compile from C# into intermediate language, which is then Just-in-time (JIT) compiled to native assembly during app launch. 

iOS:

iOS builds compile ahead-of-time (AOT) from C# into native ARM assembly code. 

Windows:

.NET MAUI uses WinUI3 library internally to create apps that target Windows. 

macOS:

Built using Mac Catalyst.

Supported Platforms

.NET MAUI platform requirements:

  • Android 5.0 (API 21) or higher.
  • iOS 10 or higher.
  • macOS 10.13 or higher, using Mac Catalyst.
  • Windows 11 and Windows 10 version 1809 or higher, using WinUI3

.NET MAUI Blazor apps have the following additional platform requirements:

  • Android 7.0 (API 24) or higher is required
  • iOS 14 or higher is required.
  • macOS 11 or higher, using Mac Catalyst.

Environment Setup

.NET MAUI is the evolution of Xamarin Forms and is embedded directly into .NET 6 framework. As such, the development environment setup has gotten simpler. 

At the time of writing this, one must install Visual Studio 2022 Preview to access .NET MAUI. MAUI is targeted for release in June 2022.

Setup Steps:

  1. Download Visual Studio 2022.
  2. Modify the installation to include .NET MAUI development workload.  
MAUI install
XAMARIN FORMS MIGRATION TO.NET MAUI

XAMARIN FORMS MIGRATION TO.NET MAUI

 .NET MAIU is the evolution of Xamarin Forms, and as such, Xamarin Forms apps will need to migrate to .NET MAUI to continuously benefit from future security updates and features. Having to rewrite existing Xamarin Forms apps as .NET MAUI apps is not an ideal choice most vendors are willing to make.

It’s in Microsoft’s best interest to retain all existing Xamarin Forms clients. Having to rewrite existing applications will not only leave a bad impression with loyal clients, but also incentivize them to explore other cross-platform development technologies like React Native, Unity, and Flutter. Thus, Microsoft has provided a framework for Migrating Xamarin Forms application to .NET MAUI. 

Xamarin Forms is a complete application framework with cross-platform UI library for mobile app development, compatible with up to .NET 5. Cross-platform development was fully integrated into .NET 6, providing a series of platform-specific frameworks as a base standard. This paradigm shift creates a single project approach to cross-platform development, in comparison to the traditional multi-project per target platform approach. 

Microsoft has provided information on how to migrate existing Xamarin Forms applications, though not without its challenges. It is also important to note, as of writing this, there isn’t any process to migrate Xamarin UWP projects, only android and iOS.

The following are the high level steps to migrating Xamarin Forms application to .NET 6:

  1. Convert the projects within a Xamarin Forms solution from .NET framework to .NET SDK style.
  2. Update namespaces.
  3. Update any incompatible NuGet packages.
  4. Address any breaking API changes.
  5. Run the converted app and verify that it functions as expected.

All documentations regarding .NET MAUI and it’s evolution can be found here: https://docs.microsoft.com/en-us/dotnet/maui/

Pre-Migration Steps:

  1. Migrating Xamarin Forms applications will need to be updated to Xamarin.Forms 4.8 or higher. 
  2. At this moment, migration on Windows using Visual Studio 2022 Preview is the recommended path by Microsoft. 
  3. Ensure the environment is setup for .NET MAUI, including all necessary workloads installed. Confirm the environment is able to build and run a blank .NET MAUI application. https://github.com/dotnet/maui/wiki#getting-started
  4. Backup the solution targeted for migration. 

Manual Migration Steps:

  1. csproj files update: In the existing Xamarin Forms application, targeting iOS and Android, there are at least three project files that will need to be updated.a
    a. Shared Common Project (csproj). Example: https://github.com/maddymontaquila/PlantLady/blob/maui-migrate/PlantLady/PlantLady/PlantLady.csproj

2. In the shared project, add a file called MauiProgram.cs and add the MauiAppBuilder: https://docs.microsoft.com/en-us/dotnet/maui/fundamentals/single-project#app-entry-point

3. In the Android Project.
Add file MainApplication.cs to this:

4. In the iOS Project.

Update Info.plist MinimumOSVersion to 15.2.
Update AppDelegate.cs to inherit from MauiUIApplicationDelegate

5. In all projects, shared and platform target projects, delete or comment out the contents on the AssemblyInfo.cs.

6. Update the following namespaces globally (VS Find and Replace is your best friend here):

7. Please note the following default padding/margin/spacing changes from Xamarin Forms.

8. Known common API changes:

    • Colors and Shapes are in Microsoft.Maui.Graphics
    • Color.Default DOES NOT EXIST == use ClearValue if you can instead 
    • Color == Colors For example: Colors.Red;
    • Frame: BorderColor = “Accent” DOES NOT EXIST
    • ToolbarItem: Icon == IconImageSource
    • Button: Image == ImageSource
    • Span ForegroundColor DOES NOT EXIST

9. Please refer to the following link on layouts and make the appropriate changes to XAML files: https://docs.microsoft.com/en-us/dotnet/maui/user-interface/layouts/

10. Update nugets in the solution:

    1. Delete Xamarin.Forms and Xamarin.Essential 
    2. Replace Xamarin.Community Toolkit with latest preview of .NET MAUI Community Toolkit: https://github.com/CommunityToolkit/Maui
    3. Replace SkiaSharp (if you are using it)  with the latest preview:
      https://www.nuget.org/packages/SkiaSharp.Views.Maui.Controls
      https://www.nuget.org/packages/SkiaSharp.Views.Maui.Core/
      https://www.nuget.org/packages/SkiaSharp.Views.Maui.Controls.Compatibility
      Please note that not all libraries used will have a convenient replacement for MAUI. A decision to migrate the existing library or use another compatible library for the same behavior needs to be made. Helpful link: https://www.nuget.org/packages/SkiaSharp.Views.Maui.Controls.Compatibility

11. The final step is usually the most challenging for larger or older Xamarin Forms applications. The final step is to fix any other outstanding errors and try to get the project to build. A helpful technique would be to comment out all the errors that can be commented out and get the solution to build. Once the projects are building, revisit the comments and fix them then  build consecutively.

For more details on migration steps: https://github.com/dotnet/maui/wiki/Migrating-from-Xamarin.Forms-(Preview)

Challenges:

The largest challenges with migration to .NET 6 are faced with incompatible external libraries. Because .NET MAUI is still in preview, only the large, more popular libraries have taken some steps to providing a compatible version. For example, many earlier Xamarin Forms projects use the PRISM library to manage MVVM pattern. To migrate PRISM based Xamarin Forms project will provide a challenge to even the microsoft MAUI team. Fortunately, PRISM now has a preview available for .NET MAUI. https://github.com/dansiegel/Prism.Maui