4
votes

I read this article a while ago: https://blog.xamarin.com/net-standard-library-support-for-xamarin/

So, I converted all of our libraries from portable libraries to .NET Standard (1.4). I did this because the article says "This PCL now needs to be upgraded to target the .NET Standard Library, which can be found in the projects properties.".

However, I can't figure out how to build a Xamarin Forms project that targets .NET Standard. I cloned the Xamarin Forms samples, and opened up the MasterDetailPage project. I went in to the project properties and switched from portable to .NET Standard as per the instructions. Immediately, I get an error telling me that I need to opt in to NuGet 3.0 support. I'm fine with this, but how do I do it?

I found that if I remove the Xamarin Forms NuGet package, I am able to switch over to .NET Standard. However, once I have done this, I can't add the Xamarin Forms NuGet package back. It just keeps failing. Contrary to what the article says, I cannot add references to .NET Standard libraries. When I try to add a reference to existing .NET Standard libraries in my Solution, Visual Studio just gives me an error saying that the library is not compatible. Which version of .NET Standard should I be targeting for Xamarin Forms?

How do I get a .NET Standard library compiling with Xamarin Forms support?

2
Unfortunately Xamarin.Forms still only supports old PCL profiles through NuGet and is not compatible with netstandard in that respect. Speaking as of today, you'd need to install Xamarin.Forms via a proxy pcl project that is consumed by a netstandard project. There are other approaches, but ideally you should just wait until it's supported by netstandard to install easily. There's a few blogs on this like the one by Oren oren.codes/2016/07/09/using-xamarin-forms-with-net-standardJon Douglas
Please tell me you are joking! My god. I've spent so much time reorganizing all of our projects and getting them to compile for .NET Standard. Why did Xamarin announce in August last year that Xamarin supports .NET Standard if we can't compile for it?Christian Findlay
I've tried following the link above, but it doesn't help in any way. I'm able to convert the Xamarin Forms project to .NET Standard 1.4, but I just get a bunch of compilation errors.Christian Findlay
Xamarin supports it just fine. Xamarin Forms however does not quite yet. It will most likely make the netstandard 2.0 release.Jon Douglas
It does support it. I've got it working. I will post my answer to my own question soon.Christian Findlay

2 Answers

5
votes

If you wanna do it with the new Visual Studio 2017 release with csproj instead of project.json you can either use dotnet migrate CLI command or add/edit this code to your csproj file:

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

  <PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8</PackageTargetFallback>
  </PropertyGroup>

</Project>
4
votes

It turns out that you can get a .NET Standard project to reference the Xamarin Forms NuGet package, and therefore be able to leverage .NET Standard libraries in Xamarin Forms.

This article more or less explains how to do it. The trick is to import the portable framework in the project.json. https://xamarinhelp.com/dot-net-standard-pcl-xamarin-forms/

If you can't get it working, here is a Git sample that does work. https://github.com/adamped/XamarinForms.NetStandard.git

I was eventually able to target .NET Standard 1.4 without issues.