How to Migrate Xamarin to MAUI

What is .NET MAUI?

  • .NET MAUI is a cross-platform framework for creating mobile and desktop apps with C#.
  • Using MAUI, you can develop apps that can run on Android, iOS, macOS, and Windows. 
  • .NET MAUI is open-source and is the evolution of Xamarin.Forms

Why Migration Needed

  • Xamarin support will end on May 1, 2024, for all Xamarin SDKs. 
  • Android 14 and Xcode 15 SDKs will be the final versions Xamarin will target.
  • .NET MAUI is a successor of Xamarin Forms. So, it is designed to improve weaknesses and add more benefits.

Set Priority During Migration

  1. Priority 1 - Give priority to just convert your Xamarin Form project into .NET MAUI; it should build and run on the device irrespective of UI & other inner functionality.
  2. Priority 2 - Once your application is running using MAUI, then you can focus on UI and inner functionality to be functional

Migration Way

  1. Upgrade Assistant - Upgrade Assistant for .NET MAUI is still under development as of Oct. 2023
  2. Manual Upgradation - We can migrate by adding the Xamarin Form codebase into the MAUI project and doing the necessary changes to remove Xamarin Form dependency.

Migration from Xamarin Forms to MAUI using Manual Upgradation

  1. Create a new blank MAUI project.
  2. Xamarin Independent Libraries- Add these libraries references directly in the MAUI project (Make sure these libraries support the MAUI project, if not, then firstly, you have to make this library compatible with the MAUI project, and then you can add them as references)
  3. Xamarin Dependent Libraries- Initially, you can add the codebase of these libraries directly in the MAUI project and make the changes to remove Xamarin Dependency.
  4. Remove Xamarin dependency if you copy the Xamarin Forms code into the MAUI project-  
    1. Copy all your Xamarin code into the MAUI project - .xaml, .cs, Assets, Fonts, Themes, and Images.
    2. Font integration - Change the build action of the font file to MauiFont and add a reference in MauiProgram.cs
    3. Change the build action of your all .xaml file from embedded resources to mauixaml, as mauixaml is the default build action for MAUI xaml pages.
    4. Replace all: xmlns="http://xamarin.com/schemas/2014/forms" to xmlns="http://schemas.microsoft.com/dotnet/2021/maui" as new namespace is the default namespace for MAUI xaml pages.
    5. Remove or change unnecessary references from .xaml & .cs pages: Xamarin & Other references, if any.
    6. Change Color.Default to new Color()
    7. Change Xamarin.Forms.Color to Microsoft.Maui.Graphics.Colors
    8. Make the placeholder changes for Renderers, Dependency Service, and custom code if any (Optional)
    9. Rebuild & Run

Note

  • Migration also depends on various factors like third-party libraries that we used, how complex are the platform-specific code, renderers, and dependency services.
  • In my project, since the start, I did not use any third-party library or write any platform-specific code, and everything is handled using default Xamarin Forms APIs; hence, the migration was a bit simpler for me.
  • These are my findings on migration, and they may vary depending on the migration approach and project structure. 

Migration Video

https://youtu.be/_E1DcFRDFiU?si=QKl53PthkiNSxO2K


Similar Articles