Referencing a PCL project in both ASP.NET Core and Xamarin Forms
Thanks to Premier Developer Consultant Gustavo Varo for this prescriptive guidance on cross-platform development with Xamarin and ASP.NET Core.
Back in March while working with Xamarin Forms and ASP.NET Core, I identified the need of sharing some code between both projects. My very first idea was to create a PCL (Portable Class Library) project and reference in both of the projects. Unfortunately, at that time both Xamarin Forms projects and ASP.NET Core projects were very different and could not support that. See bug a filed: #1356
Time has passed and a lot of things have changed. Xamarin added the support of .NET Standard Libraries and a lot of developers thought their problems were solved when trying to share libraries between Xamarin projects and other projects. The truth is that the use of standard libraries is definitely the right path since you don’t need to know upfront what clients your library needs to support, however while Xamarin does support .NET Standard libraries, Xamarin Forms still has no support on it, which has caused some confusion among Xamarin developers.
As you can see on the image below Xamarin Forms does not support .NET Standard Libraries
If you try to reference the Xamarin Forms package in a project that targets .NET Standard here is the error you will get:
Installing Xamarin.Forms 184.108.40.206.
Package Xamarin.Forms 220.127.116.11 is not compatible with netstandard1.3 (.NETStandard,Version=v1.3). Package Xamarin.Forms 18.104.22.168 supports:
– monoandroid10 (MonoAndroid,Version=v1.0)
– monotouch10 (MonoTouch,Version=v1.0)
– portable-monoandroid10+monotouch10+net45+win+win81+wp8+wpa81+xamarinios10 (.NETPortable,Version=v0.0,Profile=win+net45+wp80+win81+wpa81+MonoAndroid10+MonoTouch10+Xamarin.iOS10)
– uap10.0 (UAP,Version=v10.0)
– win81 (Windows,Version=v8.1)
– wp8 (WindowsPhone,Version=v8.0)
– wpa81 (WindowsPhoneApp,Version=v8.1)
– xamarinios10 (Xamarin.iOS,Version=v1.0)
One or more packages are incompatible with .NETStandard,Version=v1.3.
Package restore failed for ‘App1’.
Package restore failed. Rolling back package changes for ‘App1’.
To add a nugget package to a project that targets .NET Standard, the library should be .NET Standard compatible, you can see if it has dependency on the .NETStandard Library as you can see on the follow image:
Fortunately, you can still achieve your goal using .NET portable class libraries.
Here are the steps to enable that:
Create the PCL where you want to add your code that will be shared between your ASP.NET Core project and your Xamarin Forms project
Edit your PCL project and set the targets you wish your PCL to be compatible with.
On your Xamarin Forms project, uninstall the Xamarin Forms reference.
Make sure your Xamarin Forms project mimics the same targets as your PCL.
After target your Xamarin Forms project to the expected targets, add your PCL project reference to your Xamarin Forms project.
Add back the Xamarin Forms nugget package reference to your Xamarin Forms project, now you will see that the reference will be added successfully.
At this point your Xamarin Forms project should have both the Xamarin.Forms and your PCL reference.
Now on your ASP.NET Core project, try to reference your PCL project. You should be able to add the reference without any problem
Try to build your solution and done, you have a portable class library that can be shared between both your Xamarin Forms projects and your ASP.NET Core projects.
I hope this post has helped you!