Consume System.Drawing.Common package in Xamarin Android app?

larsvlarsv ✭✭Member ✭✭

My rendering code is now ported to .Net Standard 2.0 assemblies for portability.
Next step is to consume said assemblies in a Xamarin Android app.

However I do not seem to be able to consume for example the System.Drawing.Graphics type in my Xamarin code, even though I have added a reference to the System.Drawing.Common package.

Am I attempting the impossible? Must anything System.Drawing.Common be completely encapsulated in my .NET Standard assemblies? Or is there a viable workaround?

Answers

  • YorkGoYorkGo Xamurai CNMember, Xamarin Team Xamurai

    Xamarin use Mono instead of .Net, the difference between them should be clear. Mono code and .Net are almost identical, they can even be compatible with each other (.net library can be mono-referenced and used), but Mono is a separate cross-platform library. You could refer to the document: https://www.mono-project.com/docs/about-mono/compatibility/. Not all the .Net code could be used in Mono.


    System.Drawing.Graphics does not exist in Xamarin, as it conflict with Android.Graphics. You could use Android.Graphics in Xamarin.Android instead.

  • larsvlarsv ✭✭ Member ✭✭
    edited February 1

    @YorkGo - thanks for the tip, but Xamarin.Android is not portable code. The whole point (for me) of going to .Net Standard and accompanying System.Drawing.Common package is to have portable rendering code, for all platforms supported by .Net Standard. Note - this is imaging, not UI.

  • YorkGoYorkGo Xamurai CNMember, Xamarin Team Xamurai
    edited February 1

    Install the System.Drawing.Common nuget package for your portable library should be work.

  • larsvlarsv ✭✭ Member ✭✭

    @YorkGo once again appreciate the tip. That was my thinking too, but System.Drawing.Graphics is not resolved even with System.Drawing.Common package added.

    My conclusion is that it seems to not be possible to reference any symbols in the System.Drawing.Common assembly from my Xamarin code. And that's really what my question is about - should it be possible? Is there a workaround? Is what I am seeing a .Net Standard problem, or a Xamarin problem? Or am I making a user error?

    FYI: As far as I can tell the System.Drawing namespace also exists in the Mono.Android assembly. All System.Drawing classes I can reference (Color, Point, PointF, Rectangle, RectangleF, Size, SizeF) originate in the Mono.Android assembly.

    For anyone attempting to reproduce... my projects:

    • .Net Standard 2.0 assembly, that references System.Drawing.Common package, and exposes System.Drawing.Graphics as a parameter in an abstract class. This assembly builds fine. Currently building for x86, not sure if it matters at this stage.

          public abstract class MyClass
          {
              public abstract void MyMethod(System.Drawing.Graphics g);
          }
      
    • Xamarin Android class library that references the above assembly, and System.Drawing.Common package, and subclasses the abstract class above:

          public class MyAndroidClass : MyClass
          {
              public override void MyMethod(System.Drawing.Graphics g) // will not compile, Graphics not resolved
              {
              }
          }
      

    VS2017 class view for the Xamarin project shows no symbols in the System.Drawing.Common project reference. In the .Net Standard project, all System.Drawing symbols are visible in the class view.

  • larsvlarsv ✭✭ Member ✭✭

    Additionally I see a NuGet error:
    NU1201 Project mochro.android is not compatible with netstandard2.0 (.NETStandard,Version=v2.0). Project mochro.android supports: monoandroid81 (MonoAndroid,Version=v8.1)

    I guess that confirms lack of compatibility.

  • larsvlarsv ✭✭ Member ✭✭

    Found this doc page that sheds some light on the situation. Not sure yet to what degree it is relevant.

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/internals/net-standard

  • larsvlarsv ✭✭ Member ✭✭

    After some research I concluded that this is due to a problem in the net core. System.Drawing.Common.dll should be present in my deployed app folder on Android, but it's not. Manually adding it seemingly eliminates the symptom (but exposes another symptom).

    The bug report is here: https://github.com/dotnet/corefx/issues/29858
    Solution seems to be targeted for .Net Standard 3.0.

  • larsvlarsv ✭✭ Member ✭✭

    I finally got PlatformNotSupportedException from System.Drawing.Common.dll.
    Pretty clear sign. I have to write my own rendering primitives if I want portable code.

  • JGoldbergerJGoldberger Xamurai USMember, Forum Administrator, Xamarin Team, University Xamurai

    @larsv
    Sorry, I am late to the game, but I just had another customer bring this up in a support case.

    Yes, only a very small bit of System.Drawing is available in Xamarin.Android or Xamarin.iOS as you noted above.

    So yes, Xam.Android and Xam.iOS are not supported platforms, even though they are meant to be able to consume .NET Standard 2.0 assemblies, which System.Drawing.Common nuget package presents itself as supporting .NET Standard 2.0, that does not really seem to be the case. I think that System.Drawing.Common should not represent itself as being .NET Standard 2.0 compliant.

    When looking in the nuget package for System.Drawing.Common, there are folders for MonoAndroid, MonoTOuch, xamariniOS10, xamarinmac20, xamarintvos10 and xamarinwatchos10, but all of those folders just have empty .dlls (0 bytes) named .
    Only the net461 and netstandard2.0 folders have System.Drawing.Common.dll files that are > 0 bytes.

  • JGoldbergerJGoldberger Xamurai USMember, Forum Administrator, Xamarin Team, University Xamurai
Sign In or Register to comment.