Xamarin.Forms Tip - The Easiest Way To Convert A PCL Project Into .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 so far using PCL project? Well, James Montemagno has already posted this video on channel 9, however, the method mentioned there requires you to create a new project and move all your previous code from PCL project to the new .NET Standard one.

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 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.

Xamarin

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.

Xamarin

Step 4

Once done, "Save & Close" the Solution and Visual Studio. 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 this in the below image.

Xamarin

Step 6

Delete all the XML and paste the following XML there.

  1. <Project Sdk="Microsoft.NET.Sdk">  
  2.    
  3.   <PropertyGroup>  
  4.     <TargetFramework>netstandard2.0</TargetFramework>  
  5.   </PropertyGroup>  
  6.    
  7.   <ItemGroup>  
  8.     <PackageReference Include="Xamarin.Forms" Version="2.5.0.122203" />  
  9.   </ItemGroup>  
  10.    
  11.   <ItemGroup>  
  12.     <PackageReference Update="NETStandard.Library" Version="2.0.1" />  
  13.   </ItemGroup>  
  14.    
  15. </Project>  

Step 7

Open the ‘packages.config’ file in another window of Notepad,. It will look something like in the following screenshot.

Xamarin

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 a 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.

  1. <Project Sdk="Microsoft.NET.Sdk">  
  2.    
  3.   <PropertyGroup>  
  4.     <TargetFramework>netstandard2.0</TargetFramework>  
  5.   </PropertyGroup>  
  6.    
  7.   <ItemGroup>  
  8.     <PackageReference Include="SkiaSharp" Version="1.59.3" />  
  9.     <PackageReference Include="SkiaSharp.Views.Forms" Version="1.59.3" />  
  10.     <PackageReference Include="Xamarin.Forms" Version="2.5.0.122203" />  
  11.   </ItemGroup>  
  12.    
  13.   <ItemGroup>  
  14.     <PackageReference Update="NETStandard.Library" Version="2.0.1" />  
  15.   </ItemGroup>  
  16.    
  17. </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.

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.


Similar Articles