XAM110 Intro to Cross-Platform

Exercise 4: Upgrade a Portable Class Library to .NET Standard

We will work on the same project we did in the previous exercise. To explore the interoperability of Portable Class Libraries and .NET Standard class libraries, we will migrate our code from the MyTunes PCL library to our library targeting .NET Standard.

To complete the exercise, you will need Visual Studio for Windows or macOS with the Xamarin development tools installed. You will also need either an emulator/simulator or a device to run the exercise on. Please see the setup page if you need help installing the Xamarin development environment.

Open the starter solution

This exercise is a continuation of the previous exercise. You can use your existing solution or begin from the prior Exercise 3 > Completed solution in your copy of the cloned or downloaded course materials.


Migrate code from PCL

Next, we'll manually move our C# code files from the Portable Class Library to our .NET Standard library.

  1. In the Solution pad, within the MyTunes.Shared project, select the C# files and the JSON file: IStreamLoader.cs, Song.cs, SongLoader.cs, and songs.json
  2. Right-click one of these selected files and choose Copy (or hit Cmd+C)
  3. Right-click the MyTunes.NetStandard project in the Solution pad and select Paste (Cmd+V)
  1. In the Solution Explorer, within the MyTunes.Shared project, select the C# files and the JSON file: IStreamLoader.cs, Song.cs, SongLoader.cs, and songs.json
  2. Right-click one of these selected files and choose Copy (or hit Ctrl+C)
  3. Right-click the MyTunes.NetStandard project in the Solution pad and select Paste (Ctrl+V)
NOTE: The Build Action on the JSON file will also copy over, but it is good to double-check it is still set as an EmbeddedResource.

Add NuGet packages

We've copied over the source files, but in the PCL form, they were dependent on the Json.NET NuGet package. Let's add that to our .NET Standard library.

  1. In the Solution pad, within the MyTunes.NetStandard project, right-click the Dependencies node and select Add Packages…
  2. Find the Newtonsoft.Json NuGet package and click Add Package
  1. In the Solution Explorer, within the MyTunes.NetStandard project, right-click the Dependencies node and select Manage NuGet Packages…
  2. Within the Browse tab, find and select the Newtonsoft.Json NuGet package and click the Install button

To add this NuGet package via the Package Manager Console, found in the menu under Tools > NuGet Package Manager > Package Manager Console, run the following command:

Install-Package Newtonsoft.Json


Remove PCL project

  1. In the Solution pad, within each platform head project you wish to use, right-click the References node and select Edit References…
  2. Switch to the Projects tab
  3. Check the box to reference MyTunes.NetStandard
  4. Uncheck the box to remove the reference to MyTunes.Shared
  5. Click the OK button to save our changes
  1. In the Solution Explorer, within each platform head project you wish to use, expand the References node
  2. Right-click the References node itself, and choose Add Reference…
  3. Drill down to the Projects > Solution section
  4. Check the box to reference MyTunes.NetStandard
  5. Uncheck the box to remove the reference to MyTunes.Shared
  6. Click the OK button to save our changes

With these changes in place, our platform apps should be using only our .NET Standard class library. Build and run the app on your preferred platform(s) to see it still working as before.


Managing .NET Standard version

The PCL project we were using was Profile111. Officially, that profile is directly compatible with .NET Standard version 1.1.

After our experiment with HttpClient in the previous exercise, we left our .NET Standard library targeting version 1.1, which meant our new code would still compile just fine. Since we weren't really using HttpClient, we can experiment further.

  1. Open the SongNameExtensions.cs file
  2. Delete or comment out our lines dealing with HttpClient
using System;
// using System.Net.Http;

namespace MyTunes.NetStandard
{
    public static class SongNameExtensions
    {
        // public static HttpClient httpClient = new HttpClient();
        public static string RuinSongName(this string songName)
        {
            return songName.Replace("Crocodile", "Alligator");
        }
    }
}
  1. Right-click the library project in the Solution pad and chose Options.
  2. In the Project properties, under Build > General > Target framework choose .NETStandard 1.0.
  1. Right-click the library project in the Solution Explorer and chose Properties.
  2. In the Project properties, under Application > Target framework choose .NETStandard 1.0.
  1. Click the OK button to save our changes
  2. Run the app to see it is still compiling and executing fine

While .NET Standard version 1.1 is directly compatible with PCL Profile111, we aren't using any APIs that weren't available to .NET Standard version 1.0. We can reach more platforms by decrementing the .NET Standard version without any loss in functionality.

This may not always be the case when converting Portable Class Libraries, but changing the version and testing will confirm the potential for your library to have an expanded platform reach.

Exercise summary

In this exercise, you converted a Portable Class Library to target .NET Standard instead. We migrated all our PCL code to a .NET Standard project and re-added NuGet packages to get a working .NET Standard portable binary. Additionally, we saw that our .NET Standard version wasn't completely tied to the PCL profile-compatible version; it can be reduced if our API usage didn't expand to calls only available in later versions.

You can view the completed solution in the Exercise 4 > Completed folder of your copy of the cloned or downloaded course materials.

Go Back