Using Xamarin Forms with .NET Standard
With the release of .NET Core and the .NET Standard Library last week, many people want to know how they can use packages targeting netstandard1.x
with their Xamarin projects. It is possible today if you use Visual Studio; for Xamarin Studio users, support is coming soon.
Prerequisites
Using .NET Standard pretty much requires you to use project.json
to eliminate the pain of “lots of packages” as well as properly handle transitive dependencies. While you may be able to use .NET Standard without project.json
, I wouldn’t recommend it.
You’ll need to use the following tools:
- Visual Studio 2015 with Update 3
- .NET Core 1.0 for Visual Studio
- The latest Xamarin in the stable channel
Getting Started
As of now, the project templates for creating a new Xamarin Forms project start with an older-style packages.config
template, so whether you create a new project or have an existing project, the steps will be pretty much the same.
Step 1: Convert your projects to project.json
following the steps in my previous blog post.
Step 2: As part of this, you can remove dependencies from your “head” projects that are referenced by your other projects you reference. This should simplify things dramatically for most projects. In the future, when you want to update to the next Xamarin Forms version, you can update it in one place, not 3-4 places. It also means, you only need the main Xamarin.Forms
package, not each of the packages it pulls in.
If you hit any issues with binaries not showing up in your bin
directories (for your Android and iOS “head” projects), make sure that you have set CopyNuGetImplementations
to true
in your csproj
as per the steps in the post.
At this point, your project should be compiling and working, but not yet using netstandard1.x
anywhere.
Step 3: In your Portable Class Library projects, find the highest .NET Standard version you need/want to support.
Here’s a cheat sheet:
- If you only want to support iOS and Android, you can use .NET Standard 1.6. In practicality though, most features are currently available at .NET Standard 1.3 and up.
- If you want to support iOS, Android and UWP, then NET Standard 1.4 is the highest you can use.
- If you want to support Windows Phone App 8.1 and Windows 8.1, then NET Standard 1.2 is your target.
- If you’re still supporting Windows 8, .NET Standard 1.1 is for you.
- Finally, if you need to support Windows Phone 8 Silverlight, then .NET Standard 1.0 is your only option.
Once you determine the netstandard
version you want, in your PCL’s project.json
, change what you might have had:
{
"dependencies": {
"Xamarin.Forms": "2.3.0.107"
},
"frameworks": {
".NETPortable,Version=v4.5,Profile=Profile111": { }
},
"supports": { }
}
to
{
"dependencies": {
"NETStandard.Library": "1.6.0",
"Xamarin.Forms": "2.3.0.107"
},
"frameworks": {
"netstandard1.4": {
"imports": [ "portable-net45+wpa81+wp8+win8" ]
}
},
"supports": { }
}
Note the addition of the imports
section. This is required to tell NuGet that specified TFM is compatible here because the Xamarin.Forms
package has not yet been updated to use netstandard
directly.
Then, edit the csproj
to set the TargetFrameworkVersion
element to v5.0
and remove any value from the TargetFrameworkProfile
element.
At this point, when you reload the project, it should restore the packages and build correctly. You may need to do a full clean/rebuild.
Seeing it in action
I created a sample solution showing this all working over on GitHub. It’s a good idea to clone, build and run it to ensure your environment and tooling is up-to-date. If you get stuck converting your own projects, I’d recommend referring back to that repo to find the difference.
As always, feel free to tweet me @onovotny as well.
‘git clone’ worked fine. ‘dotnet build’ from the root folder on MacOS throws: Couldn’t find ‘project.json’ in current directory.
Thats a problem with dotnet build go to the directory that contains project.json and run the build otherwise do dotnet build
dotnet build
I believe that I have followed the instructions carefully but I found that quite a few of the needed assemblies are not being included in the output despite my having made the necessary changes including the CopyNuGetImplementations in the csproj. I have searched for an answer but didn’t come up with one but was wondering if you might have some additional insight. I also tried this with your example project as well, added the System.Reflection.TypeExtensions”: “4.1.0” dependency and built the project and the output bin folder does not contain the System.Reflection.TypeExtensions.dll.
What triggered me to look at this is that my project kept telling me it could not find System.Reflection.TypeExtensions”: “4.0.0” after I had added Microsoft.EntityFrameworkCore to my own project and I finally realized that no System.Reflection.TypeExtensions assembly was being included in the output even if I added both System.Reflection.TypeExtensions and Microsoft.EntityFrameworkCore to the portable and/or the Droid head project.
Thank you in advance.
Great article, very clearly explained. Thanks!
I’m targeting iOS, Android and UWP, so .NET Standard 1.4. I migrated the projects sucessfully, however it appears that there is no support for System.Resources.ResourceManager in .NET Standard 1.4.
This is a critical component for us (and others too, I assume) but not included in your sample. Is there is a solution or work around for this?
what happens if I need to support windows phone 8.1 but a library in my portable project requires netstandard 1.3 … windows phone project is refusing to add the library, any workarounds ? thx
Hy Oren,
I’ve just updated my Xamarin.Forms PCL project to netstandarda and it is compiling fine.
But now I’m having issue to reference the PCL on my Xamarin.Forms.Android native Project.
When I try to reference my Xamarin.Forms PCL on Android project I’m getting the following error:
“Unable to add reference to project ‘xxx.pcl’. The current project’s target is not one of or compatible with the targets of Portable Library project ‘xxx.pcl’
A Portable Library project’s targets can be changed via the Library tab in project’s properties.
Could you explain if you did any change to the csproj of your native Xamarin.Android project ?
Hello,
Thanks for that.
I’ve just filed an issue (https://github.com/onovotny/SampleXamFormsWithNetStandard/issues/1) because although the sample solution works on Visual Studio 15 update 3, it seems to fail with Xamarin Studio 6.1 on MacOS.
At least on my machine.
Hoping for a quick workaround to the very least.
Hi There
This is fantastic! Converting all my projects over…
I do have a question regarding Step 2 “you can remove dependencies from your “head” projects that are referenced by your other projects you reference”
Regarding Xamarin.Forms…
Does “head” project mean the project that will most likely be reference by all projects, an Abstractions project for example? In my project, I’ve separated the Views and ViewModels into their own projects, with an abstraction in between. I also have a ‘stub’ PCL for the app entry point, the only file it contains is the App.xaml file. Which project would you recommend I have the most “dependencies” in my project.json file?
thanks much,
Greg