Visual Studio 2017, .NET Standard and Xamarin

Visual Studio 2017 will change the way we work with .NET Standard. If you have .NET Standard projects now, you will have seen the project.json file. Project.json is now dead and buried, and everything has moved back into the csproj file. This also means no more .xproj file.

Upgrading from Visual Studio 2015

If you are coming from Visual Studio 2015, first I will go through a few quick upgrade scenario’s.

XProj Conversion

If you have any .xproj files, then you will be asked to automatically convert them to csproj, when you load the project in Visual Studio 2017. XProj file extensions are no longer relevant in Visual Studio 2017, they are an obsolete format.

onewayupgrade

This will remove the project.json file and put all that information into your csproj.

Portable Conversion

If you have a portable library targeting .NET Standard that has a project.json file, it will not be converted, since its already a csproj. It will continue to function the same as before. If you want to upgrade to the new .NET Standard library format, you will have to do this manually, by either modifying the csproj file, or the easy way of, creating a new .NET Standard Library and copying across your files. I will now go through how to setup a new .NET Standard Library.

.NET Standard Library

First, I will assume you installed the .NET Core tools when installing Visual Studio 2017. If not, modify your install and install the following.

net-core-tools

When you go to create a .NET Standard Library, take note of which one you should actually create. You want to create a .NET Standard Library, not a .NET Core Library.

createnetstandardlibrary

You will notice there is no project.json. Only your code and Dependencies.

netstandarddependency

If you right click and go into Properties, you will see the Target Framework is set to .NETStandard x.x.

netstandardproperties

Referencing Non .NET Standard Libraries

Since Xamarin Forms isn’t a .NET Standard Library, with project.json we had to use an imports line. Otherwise, as with the new .NET Standard Libraries, it will be unable to install a package that isn’t .NET Standard.

There is currently a bug with Xamarin Forms and .NET Standard Class Libraries. Bug 53060 – Fail to compile .NET Standard Class Library in VS2017

This is the old project.json file.

"frameworks": {
    "netstandard1.3": {
        "imports": "portable-net45+win8+wpa81"
    }
}

With Visual Studio 2017, we can Edit our csproj, without unloading it. Right click and press Edit <yourprojectname>.csproj.

editcsproj

When editing the csproj, you will see text similar to below.

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard1.3</TargetFramework>
    </PropertyGroup>
</Project>

Now, to add the imports section to the csproj, we need to add in this line.

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

Separate any other frameworks you want to support with the semi-colon( ; ) on the same line.

Xamarin Projects

Nothing changes with platform specific Xamarin Projects. You no longer need the project.json file and can use the previous methods for managing nuget packages. You can reference a .NET Standard library from any Xamarin.Android, Xamarin.iOS and UWP project, as you did previously.

Microsoft MVP | Xamarin Certified Developer |
Exrin Creator | Xamarin Forms Developer | Melbourne, Australia

Related Posts

3 Comments

  1. Gerhard Faber

    Hello Adam,
    I want to use a net standard library from an existing PCL and I get
    “C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.Common.targets(73,5): error : Für das Projekt “***.csproj” ist kein mit “.NETPortable,Version=v4.5,Profile=Profile259″ kompatibles Framework vorhanden.

    Is this error related to the bug you’ve listed. too?
    Greetings
    Gerhard

  2. Brian Limkemann

    Adam, do you have XAML Intellisense in your .NET standard shared libraries? In a normal PCL I do have it but in a NETStandard shared library I have no XAML Intellisense at all. Here are my targets and fallbacks:

    netstandard1.4
    $(PackageTargetFallback);portable-net45+net45+wp8+win81+wpa8;uap10.0;net46

Leave A Comment?