{"id":49186,"date":"2022-05-10T08:35:52","date_gmt":"2022-05-10T15:35:52","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/xamarin\/?p=49186"},"modified":"2022-05-10T20:00:58","modified_gmt":"2022-05-11T03:00:58","slug":"tips-for-porting-your-xamarin-library-to-dotnet-maui","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/tips-for-porting-your-xamarin-library-to-dotnet-maui\/","title":{"rendered":"Tips for Moving Your Xamarin Library to .NET MAUI"},"content":{"rendered":"<blockquote>\n<p>Note: This is a Guest Blog Post by <a href=\"https:\/\/luismts.com\">Microsoft MVP Luis Matos<\/a>. Luis is a System Engineer who specializes in helping companies and professionals create value in their software products.<\/p>\n<\/blockquote>\n<p>If you are a Xamarin.Forms Library maintainer and haven&#8217;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, <a href=\"https:\/\/www.nuget.org\/packages\/Plugin.ValidationRules\">Plugin.ValidationRules<\/a> to support .NET MAUI.<\/p>\n<h3>Refactor or Rewrite or Bifurcate?<\/h3>\n<p>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.<\/p>\n<p>Once you change update the Target Frameworks (TFMs) to <code>net6.0<\/code>, <code>net6.0-ios<\/code> and <code>net6.0-android<\/code>, 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\u2019t have to do anything. And in some cases, you may have to update some code.<\/p>\n<p><strong>Refactoring<\/strong> involves updating existing code to work in the new environment under .NET 6.<\/p>\n<p><strong>Bifurcation<\/strong> means copying old code and pasting it into a new .NET 6 project. If you\u2019re coming from .NET Standard you probably don\u2019t need to do this, but you may need to update some namespaces.<\/p>\n<h3>Understand your dependencies<\/h3>\n<p>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 <code>net6.0-ios<\/code> and <code>net6.0-android<\/code>.<\/p>\n<h3>Upgrade the CSPROJ Project<\/h3>\n<p>The new CSPROJ format is known as SDK style.<\/p>\n<p>\ud83d\udea8 IMPORTANT \ud83d\udea8<\/p>\n<p>If your library uses the <code>MSBuild.Sdk.Extras<\/code> SDK, you must change it to use <code>Microsoft.NET.Sdk<\/code>:<\/p>\n<pre><code class=\"language-xml\">&lt;!-- Old SDK reference --&gt;\n&lt;Project Sdk=\"MSBuild.Sdk.Extras\"&gt;\n   ...\n&lt;\/Project&gt;\n\n&lt;!-- New SDK reference --&gt;\n&lt;Project Sdk=\"Microsoft.NET.Sdk\"&gt;\n   ...\n&lt;\/Project&gt;<\/code><\/pre>\n<h3>Implement Single-Project Multitargeting<\/h3>\n<p>To learn more about <a href=\"https:\/\/docs.microsoft.com\/dotnet\/maui\/fundamentals\/single-project?WT.mc_id=dotnet-0000-bramin\">Single-Project Multitargeting<\/a>, I recommend to first <a href=\"https:\/\/docs.microsoft.com\/dotnet\/maui\/fundamentals\/single-project?WT.mc_id=dotnet-0000-bramin\">read through Microsoft&#8217;s Documentation<\/a>.<\/p>\n<p>When your projects use the SDK Style project format, you can target multiple frameworks in the same project using <code>&lt;TargetFrameworks&gt;<\/code>:<\/p>\n<pre><code class=\"language-xml\">&lt;Project Sdk=\"Microsoft.NET.Sdk\"&gt;\n  &lt;PropertyGroup&gt;\n    &lt;TargetFrameworks&gt;net6.0;net6.0-android;net6.0-ios&lt;\/TargetFrameworks&gt;\n    ...\n  &lt;\/PropertyGroup&gt;\n&lt;\/Project&gt;<\/code><\/pre>\n<h3>Fix Code Issues<\/h3>\n<p>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&#8217;ll need to correct all the compiler issues that appear.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>\ud83d\udca1TIP<\/p>\n<p>Where .NET 6 is incompatible with legacy code, you can use <code>#if !NET6<\/code> so that the old code stays exactly the same.<\/p>\n<h3>Leverage Existing Code<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3>Conclusion<\/h3>\n<p>If you want your libraries to be consumed for the .NET MAUI community you must update your library to support <code>net6.0<\/code>, <code>net6.0-ios<\/code>, and <code>net6.0-android<\/code>.<\/p>\n<p>Upgrading your codebase in most scenarios can be easy.<\/p>\n<p>And hey! You are not alone, if you need help just ask on Twitter with the hashtags <code>#dotnetmaui<\/code> <code>#dotnet<\/code> and we will be there for you. And you can always write to me on Twitter at <a href=\"https:\/\/twitter.com\/luismatosluna\">@luismatosluna<\/a>.<\/p>\n<p>I hope you find this blog post useful. A hug, and until next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Join me as I share the tips that I learned porting my NuGet Package, Plugin.ValidationRules to support .NET MAUI<\/p>\n","protected":false},"author":90770,"featured_media":49187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9211,556,1,367],"tags":[8867,8061,27],"class_list":["post-49186","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-maui","category-integrations","category-xamarin","category-xamarin-forms","tag-net-maui","tag-libraries","tag-xamarin"],"acf":[],"blog_post_summary":"<p>Join me as I share the tips that I learned porting my NuGet Package, Plugin.ValidationRules to support .NET MAUI<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/49186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/90770"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=49186"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/49186\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/49187"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=49186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=49186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=49186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}