Click here to Skip to main content
15,885,278 members
Articles / .NET

Xamarin.Forms Tip: Easiest Way to Convert a PCL Project to .NET Standard Project

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
24 Jan 2018CPOL2 min read 5K  
Easiest way to convert a PCL project to .NET standard project

Microsoft updated the Xamarin.Forms project templates in VS 2017 to use .NET Standard instead of PCL for writing the shared code (including the Xamarin.Forms UI. More details here) as NET Standard is the future of cross platform code sharing. See this blog for more information on .NET Standard.

But what about our existing applications which we have developed till now using PCL project, well James Montemagno already has this video on channel 9, however the method mentioned there requires to create a new project and move all your previous code from PCL project to new .NET Standard project.

I have another easier solution, i.e., to just change the .PROJ file XML of the PCL project to that of .NET Standard project, which I will mention in this article in a step by step manner. I will use the sample code of my old blog about using SkiaSharp with Xamarin.Forms.

Step 1

Open your existing project and you will see a screen like the following mentioning the common code project as Portable.

Image 1

Step 2 (Optional)

Update all the Nuget Packages for the solution to the latest.

Step 3

Delete ‘AssemblyInfo.cs’ present in the Properties folder of the PCL Project as shown in the below image:

Image 2

Step 4

Once done, save & close the Solution and Visual Studio as now we will be using NotePad (or any other text editor) to update the .PROJ file.

Step 5

Open the .PROJ file present in the PCL code folder in NotePad, the code will look something like the following image:

Image 3

Step 6

Delete all the XML and copy the following XML there:

XML
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Xamarin.Forms" Version="2.5.0.122203" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Update="NETStandard.Library" Version="2.0.1" />
  </ItemGroup>

</Project>

Step 7

Open the ‘packages.config’ file in another window of NotePad, it will look something like the following screenshot:

Image 4

Step 8

Now update the XML of the .PROJ file (open in previous NotePad window) by adding PackageReference in the ItemGroup tag for each Nuget Package except Xamarin.Forms (as it’s already there). Also, if you are using any 3rd party library, please check with the Package documentation about which version of .NET Standard they support before upgrading. If they don’t, then you can add PCL Compatibility Package later using Visual Studio. The XML of my project file looks like this now:

XML
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="SkiaSharp" Version="1.59.3" />
    <PackageReference Include="SkiaSharp.Views.Forms" Version="1.59.3" />
    <PackageReference Include="Xamarin.Forms" Version="2.5.0.122203" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Update="NETStandard.Library" Version="2.0.1" />
  </ItemGroup>

</Project>

Save and close the file and NotePad.

Step 9

Now open the Solution again in Visual Studio and you will see that Portable is not there like following screenshot.

Step 10

The PCL project is not converted to .NET Standard Library, just build and execute the code and you are good to go. The updated code is available on Github.

Image 5 Image 6 Image 7 Happy coding!! Image 8 Image 9 Image 10

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
Hi There, I am an IT professional with 14 years of experience in architecting, designing and building IT solutions for complex business needs in form of mobile & web applications using Microsoft technologies. Currently working in an multinational company in India as Solutions Architect. The articles here are sourced from my blog : http://techierathore.com/

Comments and Discussions

 
-- There are no messages in this forum --