Targeting .NET Standard

MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
edited January 10 in Xamarin.Forms

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?

Best Answers

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited July 5 Accepted Answer

    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://MelbourneDeveloper@bitbucket.org/MelbourneDeveloper/restclient-.net.git

    Website:
    https://bitbucket.org/MelbourneDeveloper/restclient-.net

«1

Answers

  • NMackayNMackay GBInsider, University ✭✭✭✭✭

    @ChristianFindlay

    Check this blog post, might help

    https://oren.codes/2016/07/09/using-xamarin-forms-with-net-standard/

    This is required to tell NuGet that specified TFM is compabtible here because the Xamarin.Forms package has not yet been updated to use netstandard directly.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited January 10

    @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.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    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

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    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?

  • mattwardmattward GBMember Xamurai

    What does your project.json file look like?

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited January 10

    That's correct. It ended up like this:

    {
      "supports": {},
      "dependencies": {
        "DeltaDCC": "1.0.0",
        "NETStandard.Library": "1.6.0",
        "Xamarin.Forms": "2.3.1.114"
      },
      "frameworks": {
        "netstandard1.4": {
          "imports": "portable-win+net45+wp8+win81+wpa8"
        }
      }
    }
    
  • mattwardmattward GBMember Xamurai

    @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.

     <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
    
  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    @mattward , I don't follow. What are you trying to tell me?

  • mattwardmattward GBMember Xamurai

    @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.

  • batmacibatmaci DEMember ✭✭✭✭

    @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?

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited May 14

    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.

  • batmacibatmaci DEMember ✭✭✭✭

    @MelbourneDeveloper said:
    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?

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    @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,

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭
    edited May 24

    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:

    Restoring packages for D:\Data\Work\Dev\Projects\Live\Serenocalm\Serenocalm.Forms\project.json...
    Xamarin.Forms 2.3.4.247 is not compatible with .NETStandard,Version=v1.1.
    Some packages are not compatible with .NETStandard,Version=v1.1.
    Package restore failed for 'Serenocalm.Forms'.
    Package restore failed. Rolling back package changes for 'Serenocalm.Forms'.
    

    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.

  • DirkWilhelmDirkWilhelm USMember ✭✭✭

    @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?

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @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:

    {
      "supports": {},
      "dependencies": {
        "Microsoft.NETCore.Portable.Compatibility": "1.0.1",
        "NETStandard.Library": "1.6.0",
        "PCLStorage": "1.0.2",
        "SkiaSharp": "1.58.0",
        "SkiaSharp.Views.Forms": "1.58.0",
        "Xamarin.Forms": "2.3.4.247",
        "Xamarin.Microsoft.Band": "1.3.10.1"
      },
      "frameworks": {
        "netstandard1.4": {
          "imports": "portable-win+net45+wp8+win81+wp8"
        }
      }
    }
    

    I'll attempt to do what the error suggests, to see how that goes.

  • XanderLoXanderLo CAMember

    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.

  • TimSTimS DEMember ✭✭

    @EasyGoingPat I got the same issue. Could you solve it? Adding System.Diagnostics.Tools to the project.json doesn't resolve the issue.

  • EasyGoingPatEasyGoingPat GBMember ✭✭✭

    @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.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    Welcome to Xamarin DLL hell.> @EasyGoingPat said:

    @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.

    Grab the working project here:
    https://github.com/adamped/XamarinForms.NetStandard.git

  • XanderLoXanderLo CAMember

    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!

  • Alwin.5974Alwin.5974 USMember ✭✭
    edited June 14

    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):

      • NETStandard.Library v1.6.1 (also tried v1.6.0)
      • Xamarin.Forms v2.3.4.247
      • Microsoft.NETCore.Portable.Compatibilty v1.0.1 (Have tried with and with this 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?

  • Alwin.5974Alwin.5974 USMember ✭✭

    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.

  • batmacibatmaci DEMember ✭✭✭✭

    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.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    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.

  • NamyslawSzymaniukNamyslawSzymaniuk USMember ✭✭✭

    @batmaci said:
    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.

    I've spend whole weekend, for trying to "port" my Xamarin Forms app to:

    • Profile111 (because latest releases of ACR.UserDialogs and some other 3rd party plugins didn't support Profile259), but some other 3rd party plugins didn't support Profile111 (can't remember exact which one, but most probably some sqlite)
      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 -.-

  • batmacibatmaci DEMember ✭✭✭✭

    @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?

  • AllanRitchieAllanRitchie CAInsider, University ✭✭✭

    For all the people that are having troubles moving to .NET standard, you have several options:

    1. You don't have to do it. The PCL versions of plugins work just fine
    2. Plugin devs are trying to hit .NET standard 1.0 which installs fine into PCL profile 259 (where possible). .NET standard 1.1 installs fine in PCL profile 111. https://docs.microsoft.com/en-us/dotnet/standard/net-standard
    3. If you don't like the directional shift, you can always clone the plugin projects to work with in the meantime.

    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.

  • batmacibatmaci DEMember ✭✭✭✭

    @MelbourneDeveloper I just understood your suggestion. this perfectly working. for those who needs details.

    • open class library as PCL (not .net standard)
    • Uninstall Xamarin.forms nuget
    • Right Click on project and change the target to .net standard 1.4

    Now you can add reference on main PCL project. this works fine for me.

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭

    Yep. That's right.

  • abraabra ADMember ✭✭

    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 ?

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited July 5 Accepted Answer

    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://MelbourneDeveloper@bitbucket.org/MelbourneDeveloper/restclient-.net.git

    Website:
    https://bitbucket.org/MelbourneDeveloper/restclient-.net

  • abraabra ADMember ✭✭

    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 ?

  • MelbourneDeveloperMelbourneDeveloper AUMember ✭✭✭
    edited July 8

    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:

    • Any non UI code (not Xamarin specific) should go in a single .NET Standard /PCL library that can be shared across all platforms. This library doesn't need to reference the Xamarin Forms dependencies if you want the library to be usable from platforms outside Xamarin (E.g. .NET, .NET Core). This can include interfaces etc. for factory classes that mint platform specific objects (dependency injection).
    • Any UI code (Xamarin Forms) that is not platform specific should go in a single .NET Standard / PCL. This can include interfaces etc. for UI factory classes that mint platform specific objects (dependency injection). It might have basic Xamarin Forms controls or app specific screens. You may, or may not want to combine this with the above library.
    • 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

    camera = CameraFactory.CreateCamera();
    
  • ChristoNelChristoNel USMember ✭✭

    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/

  • GordonLiGordonLi AUMember ✭✭

    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?

  • CameronMotenCameronMoten USMember
    edited August 19

    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.

        <Project Sdk="Microsoft.NET.Sdk">
    
          <PropertyGroup>
            <TargetFramework>netstandard1.4</TargetFramework>
            <PackageTargetFallback>$(PackageTargetFallback);portable-win+net45+wp8+win81+wpa8</PackageTargetFallback>
          </PropertyGroup>
    
    
        Other stuff here
        </Project>
    

    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/

«1
Sign In or Register to comment.