Converting Xamarin Native templates to .NET 6

.NET 6 is now officially out, but MAUI and the bits for .NET for Android and .NET for iOS are still in development. Some of the bits are already in preview now and are available for testing. The new SDK-style projects for Xamarin.Android and Xamarin.iOS is something I am personally very much looking forward to, as it removes a lot of clutter in the existing .csproj-files. The Xamarin Native templates naturally hasn’t been updated yet to use these, but here is how you can convert the templates by hand into the new SDK-style projects already now.

Note! These bits are still in preview and are subject to change, so the following might not be what they land on when they release in Q2 2022.

Xamarin.Android

For Xamarin.Android, create a File -> New Android App (Xamarin) project.

The Android workload might have been installed when you installed the iOS workload, but if it didn’t, run this from the command line:

dotnet install workload android

Then, replace the content of the Android .csproj-file with this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-android</TargetFramework>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Xamarin.AndroidX.AppCompat" Version="1.3.0" />
    <PackageReference Include="Xamarin.Google.Android.Material" Version="1.3.0.1" />
    <PackageReference Include="Xamarin.Essentials" Version="1.6.1" />
  </ItemGroup>
</Project>

Also, delete the AssemblyInfo.cs and the Resource.Designer.cs files. The .csproj has now been reduced down from 120 lines to just 11 lines. Nice!

Xamarin.iOS

For Xamarin.iOS it doesn’t quite seem like all the bits are in place yet. In theory you should have to do the following, but I was not able to deploy it to a device. Nonetheless, here is how you should be able to do it in theory:

Create a File -> New iOS App (Xamarin) project.

Then you’ll need to install the new iOS workload from the command line:

dotnet install workload ios

Then, replace the content of the iOS .csproj-file with this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0-ios</TargetFramework>
    <RuntimeIdentifier>iossimulator-x64</RuntimeIdentifier>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
</Project>

This should at least work for testing on simulators. We will probably see a RuntimeIdentifier targeting actual hardware when Microsoft gets closer to release.

The first time you build the project, Visual Studio will probably try to download some packages and do some XCode validation. You may also have to change the Deployment Target version to the most current one (or match the one in your .csproj). You’ll also have to delete AssemblyInfo.cs, since this is not in use with the new SDK-style projects.

1 thought on “Converting Xamarin Native templates to .NET 6”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.