This is the lab document from our Hands-On Lab: Code Reuse with .NET Standard meetup.

Part 1

Download the Phoneword app's source code from here:

https://developer.xamarin.com/samples/xamarin-forms/Phoneword/

Phoneword is a simple, but illustrative Xamarin application that demonstrates how you can easily share code across many client applications. This version uses Portable Class Libraries (PCLs) as the code sharing project of choice.

  • Open the Phoneword solution

If it helps, Unload or Remove/Delete any client projects that won't load or are no longer supported (e.g. - WinPhone, UWP, etc.)

  • Choose a client project that you can build and run
  • Run the app

That's it! Try launching the app with other clients. They all share the same code.

Part 1: .NET Standard libraries

PCLs are officially deprecated. .NET Standard libraries are the future. Lets replace the PCL with a few .NET Standard libraries.

  • Create a .NET Standard library called Phoneword.Forms
    • Delete the default Class1.cs file
    • From the project options, change the library's default Namespace to Phoneword
  • Create a .NET Standard library called Phoneword.Core
    • Delete the default Class1.cs file
    • From the project options, change the library's default Namespace to Core
  • Add Xamarin.Forms as a Nuget package reference to the Phoneword.Forms .NET Standard library
  • Move these files from the Phoneword PCL into the Phoneword.Forms .NET Standard library
    • App.xaml / App.xaml.cs
    • MainPage.xaml / MainPage.xaml.cs
    • IDialer.cs
  • Move these files from the Phoneword PCL into the Phoneword.Core .NET Standard library
    • PhoneTranslator.cs
  • Add a project reference from Phoneword.Forms to Phoneword.Core
  • DELETE the Phoneword PCL
  • Run the app

Congratulations! You just replaced a PCL with .NET Standard!

Part 1: NuGet

Imagine that Phoneword.Core contains business logic that you would like to use internally and also share with others.

NuGet packages are one of the easiest ways to share .NET code. With .NET Standard libraries, creating NuGet packages just got easier!

Let's reference Phoneword.Core as a NuGet package instead of as a project reference:

  • Remove Phoneword.Core as a project reference from Phoneword.Forms
  • Open and hand-edit the Phoneword.Core project file as follows:
<PropertyGroup>  
    <TargetFrameworks>netstandard2.0</TargetFrameworks>
    <RootNamespace>Core</RootNamespace>

    <PackageId>NashXam.Phoneword.Core</PackageId>
    <PackageVersion>1.0.0</PackageVersion>
    <Authors>YOUR_NAME</Authors>
    <Description>Awesome application core library</Description>
    <PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
    <PackageReleaseNotes>First release</PackageReleaseNotes>
    <PackageTags>nashxam xamarin netstandard</PackageTags>
  </PropertyGroup>
  • Save your changes
  • From your IDE, create the NuGet package
    • Right-click the Phoneword.Core .NET Standard library
      • Windows: Select Pack
      • Mac: Select Create NuGet Package

You just turned your .NET Standard library into a NuGet package!

Now let's use it.

  • Find your .nupkg NuGet package in Phoneword.Core > bin
  • Setup a local NuGet feed
  • Move the .nupkg to your local feed directory
  • Add your NuGet package as a reference to Phoneword.Forms
  • Run the app

The app should still look and behave exactly the same as it did when we started. Let's recap the changes we made:

  1. Split the app's shared code into two .NET Standard libraries:
    • application library - Phoneword.Forms
    • core business logic library - Phoneword.Core
  2. Replaced the shared code PCL with our .NET Standard libraries
  3. Bundled our core business logic .NET Standard library as a NuGet package
  4. Referenced our new NuGet package from our application .NET Standard library