Tips for Moving Your Xamarin Library to .NET MAUI
Note: This is a Guest Blog Post by Microsoft MVP Luis Matos. Luis is a System Engineer who specializes in helping companies and professionals create value in their software products.
If you are a Xamarin.Forms Library maintainer and haven’t yet ported your library to .NET MAUI, then you are in the right place! In this post, I will be sharing the tips that I learned porting my NuGet Package, Plugin.ValidationRules to support .NET MAUI.
Refactor or Rewrite or Bifurcate?
Your Xamarin.Forms SDK library is likely targeting .NET Standard, Xamarin.iOS and Xamarin.Android. 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.
Once you change update the Target Frameworks (TFMs) to
net6.0-android, 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 copying old code and pasting it into a new .NET 6 project. If you’re coming from .NET Standard you probably don’t need to do this, but you may need to update some namespaces.
Understand your dependencies
If your library has dependencies on other libraries or projects, you should take the time to examine their dependencies and determine what you can or cannot migrate. The external NuGet Packages that your projects depend on cannot target Xamarin.iOS or Xamarin.Android; they must instead target
Upgrade the CSPROJ Project
The new CSPROJ format is known as SDK style.
🚨 IMPORTANT 🚨
If your library uses the
MSBuild.Sdk.Extras SDK, you must change it to use
<!-- Old SDK reference --> <Project Sdk="MSBuild.Sdk.Extras"> ... </Project> <!-- New SDK reference --> <Project Sdk="Microsoft.NET.Sdk"> ... </Project>
Implement Single-Project Multitargeting
To learn more about Single-Project Multitargeting, I recommend to first read through Microsoft’s Documentation.
When your projects use the SDK Style project format, you can target multiple frameworks in the same project using
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFrameworks>net6.0;net6.0-android;net6.0-ios</TargetFrameworks> ... </PropertyGroup> </Project>
Fix Code Issues
Targeting the newer .NET 6 frameworks may cause some compilation and dependency issues. Some .NET libraries may not have versions compatible with .NET 6. You’ll need to correct all the compiler 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 replace it with a more recent, supported, library.
You can take this opportunity to refactor your existing code and add unit tests. You should be able to upgrade all your projects without changing too much of the original code.
Where .NET 6 is incompatible with legacy code, you can use
#if !NET6 so that the old code stays exactly the same.
Leverage Existing Code
Your previous code may still work on .NET 6 with a few small tweaks, 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.
If you want your libraries to be consumed for the .NET MAUI community you must update your library to support
Upgrading your codebase in most scenarios can be easy.
And hey! You are not alone, if you need help just ask on Twitter with the hashtags
#dotnet and we will be there for you. And you can always write to me on Twitter at @luismatosluna.
I hope you find this blog post useful. A hug, and until next time.