Porting to .NET MAUI your Xamarin.Forms library

Table of Contents

If you need to port to a .NET MAUI library your Xamarin.Forms library, you are in the right place. Here, the focus is on upgrading a library from Xamarin.Forms to .NET MAUI, but you will find this helpful if you need to upgrade any code to .NET 6.

Your Codebase

You have an SDK library for Xamarin.Forms targeting .NET Standard, Xamarin.iOS, and Mono because you are sharing some code between platforms. Some of your code may be ready to come along for the ride, and some may not. You will go through a process involving sorting through the projects and determining what to upgrade, abandon, or rewrite.

Refactor or Rewrite or Bifurcate?

Once you change the framework target you will have to look at each of your projects one by one and see if you have any compatibility issues. You may find that you simply don’t have to do anything. And in some cases, you may have to update some code.

Refactoring involves updating existing code to work in the new environment under .NET 6.

Bifurcation means taking the old code, copying and pasting it into a new .NET 6 project, and having it run there. If you’re coming from .NET Standard you probably don’t need to do this, but you may need to update some namespaces.

Tips for porting to .NET MAUI your Xamarin.Forms libraries

The process of porting your library from Xamarin.Forms to .NET MAUI can be easy or difficult depending on the library and the dependencies you have. Here are some tips.

TIP 1: Understand your dependencies

If your library has dependencies on other libraries or projects, you should take some time to examine them and see what you can or cannot migrate. Your external NuGet packages that your projects depend on must exist in .NET 6 as well.

TIP 2: Upgrade the Visual Studio Project (.csproj) SDK

The newer format (SDK Style) requires minimal explicit configuration and enables you to compile the code (target) for .NET Framework and .NET 6 or .NET Standard.

🚨IMPORTANT

Before building your library you had to reference the SDK of MSBuild.Sdk.Extras but now you must reference Microsoft.NET.Sdk if you want it to work.

Here an example

<!-- Old SDK reference -->
<Project Sdk="MSBuild.Sdk.Extras">
   ...
</Project>

<!-- New SDK reference -->
<Project Sdk="Microsoft.NET.Sdk">
   ...
</Project>

TIP 3: Multi-target on .NET 6

Firstly, you should read through this documentation from Microsoft.

When your projects use the SDK Style project format, you can attempt to compile to .NET 6 or .NET Standard. See this documentation on multi-targeting. In our case, it might require you to target .NET 6. If you are targeting native platforms like MonoAndroid or Xamarin.iOS you should test your code with the new .NET 6 native platform libraries.

List of frameworks and versions

Once you know your target frameworks you can do you can target the .NET 6 libraries you need.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net6.0;net6.0-android;net6.0-ios</TargetFrameworks>
    ...
  </PropertyGroup>

</Project>

TIP 4: Fix code issues

Targeting the newer .NET 6 version maybe can cause some compilation and dependency issues. Some .NET libraries may not have versions compatible with .NET 6. You need to fix all the code issues that appear. 

If .NET 6 is missing a library that you use, you will need to find an alternative or write the code yourself. The best thing is to find a more recent supported library and replace the existing code with code that uses the new library. Otherwise, you will need to use #if with different code paths for both targets.

You can take this opportunity to refactor your existing code and add unit tests, and so on. You should be able to upgrade all your projects without changing too much of the original code.

💡TIP

Where .NET 6 is incompatible with legacy code, you can use #if so that the old code stays exactly the same.

TIP 5: Don’t repeat yourself

Look for ways to avoid copying and pasting code. Your previous code may not be that different on .NET 6 so you can usually find a way to reuse your code.

Keep filenames the same during the whole process. If you move files into new folders etc. you will have a lot of difficulty with merging.

Resume

Upgrading your codebase in most scenarios can be easy. No effort at all. If you want to be updated and get all the benefits from .NET 6 you must update your target frameworks.

Also if you want your libraries to be consumed for the .NET MAUI community you must update your library.

And hey! You are not alone, if you need help just ask on Twitter with the hashtags #dotnetmaui #dotnet and we will be there for you. Also, if you need something you can always write to me on Twitter at @luismatosluna.

I hope you find this blog post useful. A hug, and until next time.

Share this content!

Facebook
Twitter
LinkedIn
Telegram
WhatsApp