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.

Note: A simpler post on how to Upgrade your PCL to a .NET Standard Class Library, in VS 2017, if that is all you want to do.

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.

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.


XAMARIN.FORMS MONTHLY NEWSLETTER

JOIN 1,000+ SUBSCRIBERS

  1. Don't miss out on updates
  2. The latest info from this site, Xamarin and the community
  3. Unsubscribe at any time*

* We use MailChimp, with double opt-in, and instant unsubscribe

Microsoft MVP | Xamarin MVP | Xamarin Certified Developer | Exrin MVVM Framework | Xamarin Forms Developer | Melbourne, Australia

Related Posts

20 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

    1. Jan Göltenboth

      The same here…After converting all PCL libs into NETStandard the Intellisense stopped working in all xaml files. For some reason it works when applying the same steps in a newly created xamarin template. In NETStandard it does not add the “DependentUpon” attribute to combine xaml and related .cs files and the generator-attribute is now set to “MSBuild:UpdateDesignTimeXaml” by default.

  3. Dirk

    Hello Adam,

    i have a forms project with project.json files like in your previous posts. After installing VS2017 i tried using dotnet migrate to migrate to the new csproj files. This worked well for the pcl and i can compile that.

    But i have a problem with the droid project (haven’t tried the ios project yet). When i try to compile that, i get this error message:

    “Cannot infer TargetFrameworkIdentifier and/or TargetFrameworkVersion from TargetFramework=’monoandroid71′. They must be specified explicitly.”

    The new csproj looks like this:

    monoandroid71
    my.app.Droid
    my.app.Droid
    win

    I don’t know how to correct this error.

    I hope you can help me with his.

    Regards,
    Dirk

    1. Adam Pedley

      With Android projects, I just leave them as the default csproj that happens when you create a new Xamarin.Android proj. I can still reference .NET Standard libraries from them. I actually don’t know how android csproj’s are meant to work if transferred to the new system.

      But if you want to continue with trying to get this working, these errors are shown when it can’t figure out the TFM, hence you need to define it manually. e.g.

      <PropertyGroup Condition=”‘$(TargetFramework)’ == ‘portable-net40+sl5+win8+wpa81+wp8′”>
      <TargetFrameworkIdentifier>.NETPortable</TargetFrameworkIdentifier>
      <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
      <TargetFrameworkProfile>Profile328</TargetFrameworkProfile>
      </PropertyGroup>

      I don’t know what that would need to be for Android though.

  4. Erich Brunner

    Hi Adam,
    Regarding the mentioned bug with Xamarin.Forms, does the described migration work for a PCL based Forms project now in VS2017 with latest stable Forms SDK?
    Best
    Eric

        1. Adam Pedley

          XF as of 2.3.5 supports .NET Standard 1.0. If you create a .NET Standard 2.0 library, you are able to add any version less than that, because it supports fewer API’s. So in answer to your question, XF already does support 2.0.

                1. Shimmy Weitzhandler

                  So can it already work?
                  I’m trying to upgrade my existing XF projects to .NET Standard, I’m not sure there is a clear article covering my case, but is it going to succeed changing the targets of the existing projects (*.csproj, VS2017), to .NET Standard 1.7 and following the other directions in your article?
                  Much appreciated Adam!

                  1. Adam Pedley

                    No, I wouldn’t use it, I doubt it will work, and if it does, it’s not production ready. I normally create all my Libraries in .NET Standard 1.3, as it covers everything I need.

                    The quickest way to upgrade an existing PCL to .NET Standard, is to just edit the csproj. Wipe out everything inside it. Add my example above. Then reload the proj, and manually add back all the nuget packages and/or references.

Leave A Comment?