Installing a PCL into netstandard Libraries (Plugins & Xamarin.Forms)

Last week we announced that Xamarin projects now support netstandard libraries, which is awesome! I made a short video on how to get started and upgrade a PCL to a netstandard library that any application could consume. The first question I got was if I could install Xamarin.Forms into a netstandard library, and I quickly said I don’t think so, because when I tried Visual Studio and NuGet got all mad at me.

This is because I didn’t read the documentation about the super hidden and fancy imports flag in the project.json that allows you to install PCLs into a netstandard library.

After your upgrade a PCL to netstandard you will get a project.json such as this:

VS 2015


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

Now, all we need to do is add a special “imports” flag and the PCLs we would like to consume under the netstandard profile


{
  "supports": {},
  "dependencies": {
    "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
    "NETStandard.Library": "1.6.0"
  },
  "frameworks": {
    "netstandard1.4": {
      "imports": [
        "portable-net45+wp80+win8+wpa81"
      ]
    }
  }
}

See the difference? That little imports flag essentially tells NuGet that this package will totally work with PCLs that support this. Now you should be good to go to install any PCL NuGet that you would like.

Now, I will say that this does seem to modify around the project a bit and the properties make it look like it is a PCL again, but it isn’t. It is all pretty new and interested in the netstandard world. I’m not going to run around and change everything from a PCL just yet, but I feel like we are getting closer!

Visual Studio 2017 Update

Now in 2017 you have a new .NET Standard project is way easier. First install the Microsoft.NETCore.Portable.Compatibility NuGet… simply edit your .csproj and you will see:

aka add the <PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8<PackageTargetFallback> to your PropertyTargets