in C#

Creating .NET Standard applications in Xamarin.Forms

Today we are going to learn how we can make use of the .NET Standard in Xamarin.Forms applications. If you don’t know what the .NET Standard is you can check out this article which details what the .NET Standard is and wants to be and more into the compatibility part with Windows applications, .NET Core and Xamarin. If you know what the .NET Standard is but still haven’t wrapped your head around it, you can check David Fowler’s explanation 😎 If you don’t know what Xamarin.Forms, you can check out this article which details how you can create a Xamarin.Forms Shared application (we will be using Xamarin.Forms PCL, but the process is the same).

You can find the whole source code for this blog post here

PCL project

We will need to convert our PCL project to a .NET Standard project. To do that we right click on the portable project and we will modify it’s settings, as displayed below.

Before we migrate our project, we need to remove all NuGet packages that depend on the PCL standard. To do that easy, we will delete the packages.config file shown in the Solution Explorer, as displayed below.

We will then click “Target .NET Platform Standard” and it should start porting it in a couple of seconds.

The latest supported .NET Standard by Xamarin.Forms 2.3.3.193 is netstandard1.4, so we will select .NET Standard 1.4 from the drop-down list as displayed below.

After all the above setup is done, the soon to be dead project.json will be created and will look something like this:

{
  "supports": {},
  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.4": {}
  }
}

The migration added the latest .NET Standard Library and the latest NETCore Compatibility layer (we don’t necessarily need it for this example, but if you have a NuGet that hasn’t been updated to .NET Core or .NET Standard, but does have PCLs inside it, this package solves the compatibility issues) that we will remove.

We now need to add the Xamarin.Forms NuGet package, and now comes the tricky part. As of Xamarin.Forms 2.3.3.193, the NuGet packages isn’t .NET Standard compatible out-of-the-box, so we need to import the necessary PCL profiles in order to work and we will modify the project.json file to look something like this:

{
  "supports": {},
  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Xamarin.Forms": "2.3.3.193"
  },
  "frameworks": {
    "netstandard1.4": {
      "imports": "portable461-win+net461+win81"
    }
  }
}

We will then remove the

  • Xamarin.Forms.Core
  • Xamarin.Form.Platform
  • Xamarin.Forms.Xaml

references from the new .NET Standard class library. We will now need to delete the packages.config files from the Android and iOS projects and replace them with project.json.

Android project

We will remove the

  • FormsViewGroup
  • System.ObjectModel
  • Xamarin.Forms.Core
  • Xamarin.Forms.Platform
  • Xamarin.Forms.Platform.Android
  • Xamarin.Forms.Xaml
  • Xamarin.Android.Support
  • Animated.Vector.Drawable
  • Xamarin.Android.Support.Design
  • Xamarin.Android.Support.v4
  • Xamarin.Android.Support.v7.AppCompat
  • Xamarin.Android.Support.v7.CardView
  • Xamarin.Android.Support.v7.MediaRouter
  • Xamarin.Android.Support.v7.RecyclerView
  • Xamarin.Android.Support.Vector.Drawable

from the References. The Android project.json will look like this (Android API 25):

{
  "dependencies": {
  },
  "frameworks": {
    "MonoAndroid,Version=v7.1": {}
  },
  "runtimes": {
    "win": {}
  }
}

iOS project

We will remove the

  • Xamarin.Forms.Core
  • Xamarin.Forms.Platform
  • Xamarin.Forms.Platform.iOS
  • Xamarin.Forms.Xaml

from the references. The iOS project.json will look like this:

{
  "dependencies": {
  },
  "frameworks": {
    "Xamarin.iOS,Version=v1.0": {
    }
  },
  "runtimes": {
    "win": {}
  }
}

We will then need to unload the iOS csproj to add the AutoGenerateBindingRedirects tag in order to get rid of the warning (there is an issue with System.Runtime 🙄). So we will add the following line into the iOS csproj:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>x

UWP project

In the UWP project we only need to update the Microsoft.NETCore.UniversalWindowsPlatform NuGet to the latest .NET Standard supported version that is 5.3.0. The UWP project.json will look like this:

{
  "dependencies": {
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.3.0"
  },
  "frameworks": {
    "uap10.0": {}
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

In conclusion

So there we have it, a fully working, up-to-date Xamarin.Forms project with .NET Standard 😎

As you may have observed, a bit of C# coding knowledge is required. For a better understanding of how powerful the C# language really is, you can check out this repository full with basic C# projects. If you want to go deeply into advanced C# topics, you can check out this repository. Stay tuned on this blog (and star the microsoft-dx organization) to emerge in the beautiful world of “there’s an app for that”.

Post image source: estatic-infotech.com

Leave a Reply