Background: I came from Java
Coming from a Java background, one of the hardest things I struggled with was understanding PCLs (Portable Class Libraries). It took a long time to figure out what the right version was, yea that magic number :(. Finding out if a Nuget package was supported in my PCL was also difficult. And then there was the case when a Nuget package could be supported, but the package author has not set things up correctly (generally because they were thinking of the desktop, but I wanted to code on the mobile). It turns out that I was not alone in this and the solution is here!
What’s the problem
Consider building a mobile app for both Android and iOS. Some of the app will be exactly the same, like talking to a REST server. Sharing that code (rather than writing it twice) is what PCLs and .NET Standard is all about. This applies to much more than mobile apps, but all places where code sharing is relevant, including libraries.
PCLs work by using magic numbers that target the intersection of APIs for a given set of devices. Yep, thats sounds confusing, I’m with you. So what is the alternative: .NET Standard!
PCLs and .Net Standard both talk about a platform. This refers to the ‘thing’ that will be running the code. A platform could be a desktop computer running Windows 10, another platform might be a laptop running Windows 8. It could be even be a chip running some IoT code. A platform is any chip that can run .NET code. A mobile phone can do more things than an IoT chip, and and desktop computer with Windows 10 can do more things than a mobile phone. It’s a sliding scale.
.NET Standard uses sensible numbers. The higher the number the more APIs that can be accessed. .NET Standard starts with the most basic APIs at 1.0 (.NET Standard 1.0) and as the numbers increase, more APIs are available. As a result the project is less portable (since the platform must support the capabilities). .NET Standard has been out for a while (approx a year as of this writing), but that first started at .NET Standard 1.0. For those who want to build mobile apps, there are a few more APIs that are needed. Accessing the file system and network being the two of the most important things for a mobile device.
In Visual Studio for Mac (on the alpha channel), it is now possible to create a .NET Standard package that targets .NET Standard 2.0. The API surface for 2.0 is a big jump, and doubles the number of APIs available from 1.6 (the next level down). This means we can code lots of great things, with much less pain. So let’s create an app to do that. The current templates for creating a Xamarin Forms app do not support it yet, so here are the steps broken down:
Trying it out
Open VS4Mac and create a new project:
Note that I’m using F# because I love it! C# can be used too Give the app a name, I’m going with
As per normal, this project will have a PCL for the shared code. Right-click and delete project. Your project structure should now look as follows: It’s now time for the exciting part; add the .NET Standard project. Right click on the solution folder, and select
Add New Project From the side menu on the left, select
Library under the heading .
NET Core, and then select
.NET Standard Library, Again I’m using selecting F# as my language because it is fantastic, but C# will work. Complete the prompts, ensuring that
.NET Standard 2.0 is selected for the target platform
And give it name, I am using the same name as the PCL
Excellent, the new project should not appear in the solution explorer along with the Droid and iOS projects. Next we need to add the
Xamarin.Forms nuget package the .Net Standard project. Expand the project structure, right click on
Dependencies and select
Add Packages, then follow the prompts as with any nuget package addition. The final step to writing this up is updating the Droid and iOS projects with a reference to the .NET Standard package. The following steps will need to be repeated for both platforms. Expand the platform, right click on ‘Reference` and select ‘Edit References…’ In the dialog that will display, check the box for the shiny new .NET Standard project: Everything is all wired up now, but there is no code in the .NET Standard project. In the .NET Standard project,
MyFirstNetStandard open the main file and add the required code to create and app. My examples will be in F#, but are easy enough to follow along if you haven’t used the language yet. Open ‘Library.fs’, and clear out anything in th file then add the namespace and open statement for Xamarin Forms
Below that, add a page with a label:
1: 2: 3: 4: 5: 6:
And finally, below the new page, create the application:
Everything should be working now, build and run you app on both Android and iOS and enjoy For more details, and those on windows, here is the MSDN announcement Happy Coding!