I read this article a while ago:
https://blog.xamarin.com/net-standard-library-support-for-xamarin/
So, I converted all of our libraries from portable libraries to .NET Standard (1.4). I did this because the article says "This PCL now needs to be upgraded to target the .NET Standard Library, which can be found in the projects properties.".
However, I can't figure out how to build a Xamarin Forms project that targets .NET Standard. I cloned the Xamarin Forms samples, and opened up the MasterDetailPage project. I went in to the project properties and switched from portable to .NET Standard as per the instructions. Immediately, I get an error telling me that I need to opt in to NuGet 3.0 support. I'm fine with this, but how do I do it?
I found that if I remove the Xamarin Forms NuGet package, I am able to switch over to .NET Standard. However, once I have done this, I can't add the Xamarin Forms NuGet package back. It just keeps failing. Contrary to what the article says, I cannot add references to .NET Standard libraries. When I try to add a reference to existing .NET Standard libraries in my Solution, Visual Studio just gives me an error saying that the library is not compatible. Which version of .NET Standard should I be targeting for Xamarin Forms?
How do I get a .NET Standard library compiling with Xamarin Forms support?
Answer Found!
The main thing I was doing wrong was trying to use the Visual Studio front end to switch over to .NET Standard. You can't do it that way. You have to manually create and edit the project.json. This article helped a lot:
https://xamarinhelp.com/dot-net-standard-pcl-xamarin-forms/
If you still can't get it working, there is a Git Repo which has a working version here:
https://github.com/adamped/XamarinForms.NetStandard.git
In the end, I was able to target .NET Standard 1.4. I was able to reference my existing .NET Standard 1.4 libraries, and this runs on at least Windows UWP. I'm now going to test the other platforms.
@mattward , it was just the standard that comes with the MasterDetailPage sample: none. It was using the old packages.config version of NuGet. Part of the solution, as mentioned above is to switch to the project.json. Apparently, Microsoft is going to ditch project.json soon though so I look forward to more problems when that happens.
Your project.json file should look something like this.
{ "supports": {}, "dependencies": { ... }, "frameworks": { "netstandard1.4": { "imports": "portable-net45+win8+wpa81+wp8" } } }
It depends on how your solution is structured.
I have a sample solution with a base .NET Standard/PCL library (CF.RESTClient.NET.Standard). On top of that, I have another sample app that is Xamarin Forms - .NET Standard/PCL (CF.RESTClient.NET.Sample). This project references Newtonsoft's JSON parser. Then, all of the platform specific projects (iOS, UWP, Android) references that project. Each platform specific project also references Newtonsoft. I believe that this is a good structure.
It means that I can keep clean .NET Standard logic (not Xamarin Forms) in the lowest library (CF.RESTClient.NET.Standard). This can be shared by a .NET/.NET Core app as well. But, all of the UI is in the shared .NET Standard Xamarin Forms library CF.RESTClient.NET.Sample so I don't need to write any platform specific UI.
Clone the repo here:
https://[email protected]/MelbourneDeveloper/restclient-.net.git
Website:
https://bitbucket.org/MelbourneDeveloper/restclient-.net
Answers
@ChristianFindlay
Check this blog post, might help
https://oren.codes/2016/07/09/using-xamarin-forms-with-net-standard/
@NMackay , sorry, but this doesn't help. I was able to follow some of the steps in this post:
https://oren.codes/2016/02/08/project-json-all-the-things/ to convert the Xamarin Forms project to NuGet 3.0. Once I did that, I was able to convert the Xamarin Forms project to .NET Standard 1.4 without any errors. But, The project doesn't compile at all. It appears that the project won't accept the Xamarin Forms libraries inside the NuGet package because everything related to Xamarin Forms causes a compilation error.
One of the compilation errors is this:
Package Xamarin.Forms 2.3.3.180 is not compatible with netstandard1.4 (.NETStandard,Version=v1.4). I get the same error in .NET Standard 1.6 as well.
Severity Code Description Project File Line Suppression State
Error Package Xamarin.Forms 2.3.3.180 is not compatible with netstandard1.4 (.NETStandard,Version=v1.4). Package Xamarin.Forms 2.3.3.180 supports:
- monoandroid10 (MonoAndroid,Version=v1.0)
- portable-monoandroid10+net45+win+win81+wp8+wpa81+xamarinios10+xamarinmac20 (.NETPortable,Version=v0.0,Profile=win+net45+wp80+win81+wpa81+MonoAndroid10+Xamarin.iOS10+xamarinmac20)
- 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) 0
So, can I basically take it that you cannot create a Xamarin Forms library for .NET Standard?
This is a MASSIVE problem. Why was it advertised that Xamarin would support .NET Standard 6 months ago, and there is still no .NET Standard support for Xamarin Forms?
What does your project.json file look like?
Answer Found!
The main thing I was doing wrong was trying to use the Visual Studio front end to switch over to .NET Standard. You can't do it that way. You have to manually create and edit the project.json. This article helped a lot:
https://xamarinhelp.com/dot-net-standard-pcl-xamarin-forms/
If you still can't get it working, there is a Git Repo which has a working version here:
https://github.com/adamped/XamarinForms.NetStandard.git
In the end, I was able to target .NET Standard 1.4. I was able to reference my existing .NET Standard 1.4 libraries, and this runs on at least Windows UWP. I'm now going to test the other platforms.
@mattward , it was just the standard that comes with the MasterDetailPage sample: none. It was using the old packages.config version of NuGet. Part of the solution, as mentioned above is to switch to the project.json. Apparently, Microsoft is going to ditch project.json soon though so I look forward to more problems when that happens.
Your project.json file should look something like this.
That's correct. It ended up like this:
@ChristianFindlay - In .NET Core projects, which support creating .NETStandard libraries and are what is the planned replacement for using project.json files, the imports in the project.json file maps to a PackageTargetFallback element in the .csproj file.
@mattward , I don't follow. What are you trying to tell me?
@ChristianFindlay - You said 'Apparently, Microsoft is going to ditch project.json soon though so I look forward to more problems when that happens.' so I was just talking about what you may need to do in the future if you switch away from project.json. Although I don't think there are any plans to cause project.json to stop working in the future.
@MelbourneDeveloper what is the main advantage of using .net libraries? I always think some of nuget packages might not support .net standard. so in this case is it possible to mix? I mean are we still able to use that package doest support .net standard?
I like .NET standard because it is supported by all platforms except for Silverlight. In Xamarin, you still have to target PCL in your .NET Standard project, but basically, you can use the same DLL over just about everything: .NET, Android, iOS, UWP, .NET Core etc.
for example. i am using Freshmvvm and it doesnt support .net standard, does it mean if I convert to use .net standard, I will not able to use Freshmvvm anymore. Vice versa, my PCL project is .net standard, Sensors package uses .net standard, am I not able to use it until I convert my PCL into .net standard?
@batmaci ,
I don't really understand your questions. Here's how you can look at it:
.NET Standard is simply a set of APIs. They are versioned. You can target this assembly format and reference it from most platforms except for Silverlight. However, some libraries will need the assembly to also target PCL. A .NET Standard library can target PCL as well. See the answers for this thread to see how to do that,
Does anyone know if there has been any movement on this? I have tried uninstalling all nuget packages, checked I am on nuget V3, switched all my PCLs to target .NET Standard, and Xamarin.Forms claims not to be compatible and fails to install.
I have tried .NET Standard 1.5, 1.4, 1.3 and 1.1, all with the same result shown below:
Does anyone know how to fix this?
Incidentally, I am trying to fix this because SkiaSharp will not install in a project that targets .NET 4.5 (see https://forums.xamarin.com/discussion/96229/how-do-i-get-skiasharp-to-build-now-i-have-updated-xamarin).
Any help anyone? Completely stuck here. I've had a project that's been building for months, then a Xamarin update broke it yesterday and I can't get a working build at all. Spent about six hours on this so far. Life's just too short for this s**t.
@EasyGoingPat
It should work if you added the following line as described in the accepted answer:
"imports": "portable-net45+win8+wpa81+wp8"
Can you show us your project.json?
@DirkWilhelm Thank you for the reply.
I must have had something wrong. I have now got further than I got before with the .json file posted below. Unfortunately, I now get 20-30 cases of the following error:
3>D:\Data\Work\Dev\Projects\Live\MyProject\MyProject.Forms\obj\Debug\MyProject.Forms.Views.SettingsView.xaml.g.cs(18,42,18,64): error CS1069: The type name 'GeneratedCodeAttributeAttribute' could not be found in the namespace 'System.CodeDom.Compiler'. This type has been forwarded to assembly 'System.Diagnostics.Tools, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
.json file:
I'll attempt to do what the error suggests, to see how that goes.
Using VS 2015 Update 3, Xamarin.Forms 2.3.4.247 and .NET Standard 1.4 (followed the project.json conversion tutorial), I also get "CodeDom does not exist in this namespace".
I simply created a Xamarin cross-platform app with the Master Detail template, and attempted conversion to .NET Standard 1.4.
@EasyGoingPat I got the same issue. Could you solve it? Adding System.Diagnostics.Tools to the project.json doesn't resolve the issue.
@XanderLo @TimS No, I haven't managed to fix this. I've had to switch to another non-Xamarin project because of a deadline, so I'm kind-of hoping that all this malarkey will be fixed 'properly' when I get back to it. If you find a workaround, I would love to hear it.
Welcome to Xamarin DLL hell.> @EasyGoingPat said:
Grab the working project here:
https://github.com/adamped/XamarinForms.NetStandard.git
Hi All,
I actually managed to fix those compilation issues by installing the latest .NET Core SDK and .NET Core VS2015 Tools Preview 2.
Thanks!
I had trouble converting PCL to .NET standard so I created a brand new project to test. I have got it down to one error now when compiling:
CS0246 The type or namespace name 'NeutralResourcesLanguageAttribute' could not be found (are you missing a using directive or an assembly reference?) (in AssemblyInfo.cs)
The only way I can get it to compile it to manually comment out the line below in AssemblyInfo.cs, is this something I need to be concerned about?
[assembly: NeutralResourcesLanguage("en")]
This is my config:
Installed packages (only these are installed):
Target: I have tried .NETStandard 1.1, 1.3, 1.5
Project.json:
{ "supports": {}, "dependencies": { "Xamarin.Forms": "2.3.4.247", "NETStandard.Library": "1.6.0", "Microsoft.NETCore.Portable.Compatibility": "1.0.1" }, "frameworks": { "netstandard1.3": { "imports": "portable-net45+win8+wpa81+wp8" } } }
Visual Studio 2015 SP3
I have no other references and the project is empty.
In visual studio have cleaned, reloaded, and run VS "as admin"
UPDATE:
Actually, in addition to the 'NeutralResourcesLanguage' issue/workaround above, as soon as I add a content page to the project I get a compilation error for every control on the page (I see other posts above have mentioned this problem too):
CS1069 The type name 'GeneratedCodeAttributeAttribute' could not be found in the namespace 'System.CodeDom.Compiler'. This type has been forwarded to assembly 'System.Diagnostics.Tools, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly.
and then, for that page:
Build action 'EmbeddedResource' is not supported by one or more of the project's targets.
Has anyone actually got Xamarin working with .NETStandard?
Have you seen this article? https://oren.codes/2017/04/23/using-xamarin-forms-with-net-standard-vs-2017-edition/
Ok, so it looks as though this is for VS2017 only then? I don't have it installed yet but try again when I have it.
Can somebody please explain me this bullshit? Why most the of the libraries require to use profile259 instead of profile111 if windows phone8.0 is deprecated. Only difference between them is WP8.0 that profile111 doesn't have it. Please dont tell me that change your project to .net standard instead of xamarin profile. this is not easy for existing project if some nugets like freshmvvm doesnt support yet .net standard.
Hahaha. This stuff puzzles me too. But, it's not all that complicated. I just create a .NET Standard library (1.4) that also targets PCL. That way it works on .NET, iOS, Android, UWP, and .NET Core.
Here are a few repos where I have sample apps that leverage a .NET Standard library across several .NET/Xamarin platforms:
https://github.com/MelbourneDeveloper/SQLite.Net.Standard (Tested)
https://bitbucket.org/MelbourneDeveloper/restclient-.net (Tested)
https://github.com/MelbourneDeveloper/Adapt.Presentation (Maybe tested?)
You can use theses samples to create a solution with multiple platforms that all leverage the same Xamarin Forms .NET Standard library.
I've spend whole weekend, for trying to "port" my Xamarin Forms app to:
then to:
-.NET Standard 1.7 (latest stable .NET Standard), but some other 3rd party plugins also didn't support .NET Standard...
Exerything (reinstalling nugets, changing profile, workarounding whole project for .NET Standard) took soo long, and it's so f..ing annoying -.-
@MelbourneDeveloper I have already created .net standard 1.4 class library but my XF project has profile111 and when I compile it, it complains that it cant work with Profile111. It requires 259, I dont know what else do you mean?
For all the people that are having troubles moving to .NET standard, you have several options:
For people that are having trouble with Xamarin Forms:
.NET standard is only supported 2.3.5 which is in preview on nuget. 2.3.4 is only PCL.
The fact is, the industry is moving this way. OSS is going to move with it because we generally like to be up to date and we don't have time to support the past. There is no doubt that tooling and plan around .NET standard has been a mess to say the least, but I think Microsoft is almost there.
@MelbourneDeveloper I just understood your suggestion. this perfectly working. for those who needs details.
Now you can add reference on main PCL project. this works fine for me.
Yep. That's right.
I have successfully converted a PCL project to .NetStandard. Now I need to add a new NuGet package (for instance Newtonsoft.Json). Do I have to add the NuGet package to the .NetStandard DLL project, and also to each of the *.Android, *.UWP and *.iOS projects ?
It depends on how your solution is structured.
I have a sample solution with a base .NET Standard/PCL library (CF.RESTClient.NET.Standard). On top of that, I have another sample app that is Xamarin Forms - .NET Standard/PCL (CF.RESTClient.NET.Sample). This project references Newtonsoft's JSON parser. Then, all of the platform specific projects (iOS, UWP, Android) references that project. Each platform specific project also references Newtonsoft. I believe that this is a good structure.
It means that I can keep clean .NET Standard logic (not Xamarin Forms) in the lowest library (CF.RESTClient.NET.Standard). This can be shared by a .NET/.NET Core app as well. But, all of the UI is in the shared .NET Standard Xamarin Forms library CF.RESTClient.NET.Sample so I don't need to write any platform specific UI.
Clone the repo here:
https://[email protected]/MelbourneDeveloper/restclient-.net.git
Website:
https://bitbucket.org/MelbourneDeveloper/restclient-.net
Ok, thanks.
That means the recommendation is that Xamarin.Forms should be used only in the shared part. The Xamarin.Forms NuGet package is then installed only in the *.Android, *.UWP and *.iOS projects, and the .NetStandard dll should not use Xamarin.Forms.
I have an application which has in the .NetStandard dll some bootstraper code and some navigation service class that need Xamarin.Forms. Should I move them to the shared project, or create a second .Net Standard dll, which would be dependent on Xamarin.Forms ?
I hope I'm not miscommunicating things here. There are many ways to structure your app. It's just that my basic guidelines are like this:
Lastly, you will need your platform specific libraries. This is for the bits and pieces that are not generic across all platforms. They may be custom renderers for your controls, or they may just implement functionality that is specific to the platform. Often I try to implement the factory interfaces from the above libraries so that the factory classes can be used to mint objects seamlessly without needing logic like
if (Android)
{
camera = new AndroidCamera();
}
Instead you'd have logic like
I'm just going to leave this here:
Its allot newer and you don't have to do most of the things you guys mention here:
https://blog.xamarin.com/building-xamarin-forms-apps-net-standard/
after upgrade to VS 2017 15.3 , this is not working . the import since like stop working as the error message about the Xamarin.forms not targeting to .net Standard come back.
the project working well in 15.2 . any Idea?
Note: I work for Microsoft, but this is not representative of the company as an offical answer. I just do Xamarin in my free time.
This might help..
Create a new ".NET Standard Library"
Then edit the .csProj file to the following.
What this basically does is tell VS that you are accepting/compatible of these other frameworks... This allows you to download nuget packages and it should work the same as the no longer working project.json files above.
I put a link to the test project if your interested to see full copy.
(Download at)
https://www.cameronmoten.com/2017/08/19/fixing-xamarin-with-net-standard-in-visual-studio-2017-v-15-3/