Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms - Targeting .NET Standard

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?

like image 250
Christian Findlay Avatar asked Jan 04 '23 19:01

Christian Findlay


2 Answers

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>
like image 100
Viktor Hofer Avatar answered Jan 08 '23 06:01

Viktor Hofer


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.

like image 44
Christian Findlay Avatar answered Jan 08 '23 07:01

Christian Findlay