locked
Xamarin Forms and .Net Standard 2.0 compatibility RRS feed

  • Question

  • User372700 posted

    I have been trying to develop a Xamarin application making use of Xamarin Forms and .Net Standard 2.0 class libraries, but I am finding that they seem to be very buggy. I was under the impression that Xamarin / mono framework is supposed to be completely .net standard 2 compliant.

    As a simple example, i created a new xamarin forms project in VS2017 Pro (15.8.2), with code sharing strategy set to .Net Standard. I then added a new .net standard class library to the solution and referenced it from the .Android project. In the class library i created a simple Enum and a simple class with a function that takes an enum as a parameter and uses the .HasFlag to check for a value. In the xamarin forms main page i added a button which when pressed creates an instance of the class from the .net standard class library, and calls the function with a fixed value from the enum. When i run this code in the Android simulator it works perfectly, however as soon as I try to run it on my actual Android device it fails on the .HasFlag call with the error "Object of type System.Int32 cannot be converted to type System.Enum".

    The simulator Android OS version i tried was the latest 8.1 and also tested on 4.4. The device i was testing on was also 4.4. I was using Xamarin Live Player to deploy and test.

    So my question is why does this simple thing fail on the device?

    This is not the only issue I have faced, I also found that the System.Drawing namespace that is part of the .NetStandard 2 spec also fails. I was just using it for the Color definitions and the Rectangle and Point structures as I was intending to do some rendering with SkaiSharp. I know SkiaSharp has its own versions of these so i was able to effectively remove all uses of those classes, however it again seems to indicate another .net standard 2 thing that doesnt actually work with xamarin.

    I feel like I must be missing something obvious, Xamarin has been around for quite a while as far as I know and I would expect these sort of issues would affect others too if they were legit bugs.

    I have attached a copy of the sample solution that is generating the .HasFlag error on my Android device if anyone needs it, otherwise its easy enough to repo from a new project as explained.

    Any help would be much appreciated, Cheers

    FYI my VS2017 Xamarin Versions are Xamarin 4.11.0.756 Xamarin Designer 4.14.221 Xamarin Templates - 1.1.116 Xamarin.Android SDK 9.0.0.19 Xamarin.iOS and Xamarin.Mac SDK 11.14.0.13 Mono Debugging for VS 4.11.8-pre

    Thursday, September 6, 2018 12:49 PM

Answers

  • User364855 posted

    @cjorgens79

    You need to turn on Developer options and enable USB debugging in your real device. Usually, you could find Developer options by going to Settings > About phone and tap Build Number repeatedly until developer mode is enabled. Then connect your phone to your PC, there will pop up a window on your phone need you to trust your PC and trust it. After that, the visual studio could detect your phone. Press it and it will deploy the project to your phone then start debugging.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, September 7, 2018 8:28 AM

All replies

  • User364855 posted

    @cjorgens79

    The device i was testing on was also 4.4. I was using Xamarin Live Player to deploy and test. There are many limitations for Xamarin Live Player: https://docs.microsoft.com/en-us/xamarin/tools/live-player/limitations The Xamarin Live Player is a good tool for testing your UI, but not good for your program code. If you want to test your project on a real device, please try to use a USB cable to connect your phone with the computer. I have tested your code, it works fine on my real device.

    Friday, September 7, 2018 5:47 AM
  • User372700 posted

    @BillyLiu said: @cjorgens79 There are many limitations for Xamarin Live Player: If you want to test your project on a real device, please try to use a USB cable to connect your phone with the computer. I have tested your code, it works fine on my real device.

    Good to know re the live player, thanks for that info. I did try run it directly on the device as well (by copying the apk and installing it, but it doesn't start at all). Did you have to do anything to get my sample project working like using a different signing certificate? Should it be possible for the app be run and debugged over USB on the device without using the Xamarin Live Player? The only options I have for deployment/debug targets are either simulator or xamarin live player targets, i don't get an option for a real device.

    EDIT: never mind re the debugging on device, i've found some doco so will give it a try to see if it works for me

    Friday, September 7, 2018 7:11 AM
  • User364855 posted

    @cjorgens79

    You need to turn on Developer options and enable USB debugging in your real device. Usually, you could find Developer options by going to Settings > About phone and tap Build Number repeatedly until developer mode is enabled. Then connect your phone to your PC, there will pop up a window on your phone need you to trust your PC and trust it. After that, the visual studio could detect your phone. Press it and it will deploy the project to your phone then start debugging.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, September 7, 2018 8:28 AM
  • User372700 posted

    Thanks for your help, got it sorted now :) My problem was thinking that Live Player was the way to test/debug android devices, once i enabled developer mode on the device then it appeared in VS as a normal device to target.

    Friday, September 7, 2018 12:46 PM