.NET Standard 2.0 Support in Xamarin.Forms
Let us know how you feel about this
last updated: 2017-10
This article explains how to convert a Xamarin.Forms application to use .NET Standard 2.0.
.NET Standard is a specification of .NET APIs that are intended to be available on all .NET implementations. It makes it easier to share code across desktop applications, mobile apps and games, and cloud services, by bringing identical APIs to the different platforms. For information about the platforms supported by .NET Standard, see .NET implementation support on Microsoft Docs.
.NET Standard libraries are the replacement for Portable Class Libraries (PCL). However, a library that targets .NET Standard is still a PCL, and is referred to as a .NET Standard-based PCL. Certain PCL profiles are mapped to .NET Standard versions, and for profiles that have a mapping, the two library types will be able to reference each other. For more information, see PCL compatibility on Microsoft Docs.
.NET Standard project support is still developing in Visual Studio for Mac. You may observe issues with file nesting, file reference duplication, and other oddities. Much of this may be worked around manually by removing those references in the .csproj file. However, it's recommended to wait for an update for Visual Studio for Mac. The work to rectify this situation can be tracked here.
Xamarin.Forms 2.4 allows Xamarin.Forms applications to target .NET Standard 2.0 by replacing the PCL or shared project with a .NET Standard 2.0 library. This can be achieved as follows:
- Ensure .NET Core 2.0 is installed.
- Update the Xamarin.Forms solution to use Xamarin.Forms 2.4, or greater.
- Add a .NET Standard library to the solution, that targets .NET Standard 2.0.
- Delete the class that's added to the .NET Standard library.
- Add the Xamarin.Forms 2.4 (or greater) NuGet package to the .NET Standard library.
- In the platform projects, add a reference to the .NET Standard library and remove the reference to the PCL or shared project that contains the Xamarin.Forms user interface logic.
- Copy the files from the PCL or shared project to the .NET Standard library.
- Remove the PCL or shared project that contains the Xamarin.Forms user interface logic.