.NET Standard libraries bring our code to more platforms and give us even more APIs from the Base Class Libraries with which to build amazing apps. Developers have been able to use .NET Standard libraries with Xamarin to share code across platforms, and use Xamarin.Forms to create a shared user interface across iOS, Android, UWP, macOS, for a long time. When combined, .NET Standard and Xamarin.Forms are a match made in heaven for maximizing the amount of code we can share across platforms, both in our underlying business logic as well as our user interface layer.
In this blog post, you’ll learn how to combine .NET Standard libraries and Xamarin.Forms to maximize code sharing and productivity.
What is this .NET Standard thing?
.NET Standard is a formal specification of .NET APIs that are intended to be available on all .NET runtimes (such as .NET Framework, Mono, and .NET Core). In real terms, you can think of this as a simplified, yet expanded, Portable Class Library. Any code added to a .NET Standard library can be used on any runtime that supports the .NET Standard Platform. In addition, we get expanded access to APIs within the .NET base class libraries, support more platforms, and we don’t ever have to deal with the madness that is PCL profiles.
Bringing Xamarin.Forms to .NET Standard
The Xamarin.Forms 2.3.5-pre release added compatibility with .NET Standard. In addition to shared projects and Portable Class Libraries, you can now add .NET Standard libraries to your code sharing tool belt.
File -> New
Let’s get started by creating a new Xamarin.Forms app. If you already have an existing Xamarin.Forms app you’re looking to migrate to a .NET Standard, you can skip directly to the next step.
In Visual Studio, click File -> New Project -> Cross-Platform -> Cross-Platform App. Select Master Detail, and click OK. This is going to create a scaffolded mobile app for iOS, Android, and UWP with Xamarin.Forms that displays some data in a list—a common mobile development recipe.
In Visual Studio for Mac, you can create a similar template by going to File -> New -> Multiplatform -> Forms App.
Adding a .NET Standard Library
Adding a .NET Standard library to your app is super easy; it works just like adding any other library to your project!
If you’re using Visual Studio 2017, right-click the solution and click Add -> New Project. Navigate to the .NET Standard section and add a .NET Standard project.
Developers using Visual Studio for Mac can create a .NET Standard library by navigating to Multiplatform -> Library -> .NET Standard Library. If you’re using Visual Studio 2015, be sure to follow our getting started guide to create and convert a Portable Class Library to a .NET Standard library.
Adding Xamarin.Forms to a .NET Standard Library
Just like Portable Class Libraries, we can add NuGets to .NET Standard libraries. Open the NuGet Package Manager by right-clicking the project and selecting Manage NuGet Packages. Switch to the Browse tab, select Include prerelease, and search for Xamarin.Forms. At the time of this blog post, only the prerelease version of Xamarin.Forms has compatibility with .NET Standard, although it is likely that .NET Standard compatibility will be included in the next stable release.
In each of the other projects (such as iOS, Android, UWP, etc.), upgrade to the latest prerelease Xamarin.Forms NuGet, add a reference to the .NET Standard library we created, and remove the reference to the shared project or PCL that contains your Xamarin.Forms UI logic.
Finally, if you’re migrating, copy over files from your shared project or PCL to our .NET Standard library. That’s it! You’re now using a .NET Standard library with Xamarin.Forms to share code:
Want to see it in action? Our very own James Montemagno walks through the upgrade path on The Xamarin Show on Channel 9 with plenty of tips and tricks along the way:
In this blog post, we created a brand new cross-platform mobile app and used a .NET Standard library as our code sharing strategy to reach all the platforms. To learn more about .NET Standard for Xamarin (without Xamarin.Forms), check out our blog on .NET Standard support. If you’re still curious as to what .NET Standard is all about, I suggest you check out Immo Landwerth’s introductory video, read up on the documentation on docs.microsoft.com, or follow along on GitHub.