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.
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:
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:
Step 6
Delete all the XML and copy the following XML there:
<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:
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:
<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.
Happy coding!!