Using PCL only libraries with .Net Standard
The wait will soon be over and we will all be able to use .Net Standards for sharing all our code across .Net run-times. But for the current time we are still in a transition state, not all libraries support .Net Standard out of the box. One option would to rely on good old Portable Class Libraries (PCLs) for now. But the PCL comes with many restrictions compared to a .Net Standard library. Plus .Net Standard is where all the action is (and will be) happening. So it seems like a no brainer to choose .Net Standard as our library container to share code.
But what when we are using a library that does not support .Net Standard out of the box? For instance let’s say we want to use Reactive UI as our MVVM framework for a Xamarin.iOS and Xamarin.Android app. When trying to add Reactive UI version 7.4.0 (latest stable version as of writing) via NuGet to our .Net Standard Project. We get the following error message:
The solution to our problem is to define the required targets in our .Net Standard Project. The targets which the Reactive UI NuGet package defines. We can edit a .Net Standard library by right clicking on it in Visual Studio and selecting Edit Project name:
Now we can define the targets by adding the PackageTargetFallback line to our DotNetStandard.Core.csproj file:
Providing the targets provides NuGet with the information needed to install the package. Rerunning the NuGet installation once more leads to a success
Conclusion
.Net Standard is the future of writing .Net Libraries which runs on multiple .Net runtime Environments.
Happy coding and don’t let those not-yet ported libraries stop you from achieving your goals!