Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type resolution with Splat library and Xamarin.Mac

I have an Xamarin.iOS project that uses the splat library https://github.com/paulcbetts/splat to make System.Drawing types available in portable class library. If a parent class uses (say) System.Drawing.RectangleF, then by using Splat, it works just fine to subclass this class in Xamarin.IOS code. However, the same is not true of Xamarin.Mac, at least not the way I am doing it. Various types conflict with themselves -- at a minimum Point and RectangleF.

I don't know if this is related to Xamarin's recent updates (to Xamarin 6) or not.

Some sample code is below, and I'm making a full project demonstrating the problem available on Github. https://github.com/verybadcat/splat -- macbug branch.

It looks similar to the problem described here [Splat [0.3.4] on Xamarin.iOS: issues with RectangleF and PointF.

Portable Class Library project:

using System.Drawing;
namespace PCL
{
  public class RectOwner
  {
    public RectangleF Rect { get; set;}
  }
}

IOS project -- this works just fine:

using PCL;

namespace IOSApp
{
  public class RectOwnerIOS: RectOwner
  {
    public RectOwnerIOS ()
    {
      this.Rect = new System.Drawing.RectangleF (10, 20, 30, 40);
    }
  }
}

Mac project -- does not build:

using PCL;


namespace MacApp
{
  public class RectOwnerSubclass: RectOwner
  {
    public RectOwnerSubclass ()
    {
      this.Rect = new System.Drawing.RectangleF (5, 6, 7, 8); // errors here:
      // /Users/william/Documents/splat/MacApp/RectOwnerMac.cs(16,16): Error CS7069: Reference to type `System.Drawing.RectangleF' claims it is defined assembly `Splat, Version=1.6.2.0, Culture=neutral, PublicKeyToken=null', but it could not be found (CS7069) (MacApp)
     // /Users/william/Documents/splat/MacApp/RectOwnerMac.cs(23,23): Error CS0029: Cannot implicitly convert type `System.Drawing.RectangleF [Xamarin.Mac, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]' to `System.Drawing.RectangleF [Splat, Version=1.6.2.0, Culture=neutral, PublicKeyToken=null]' (CS0029) (MacApp)
    }
  }
}

How can I get the Mac project to build?

like image 258
William Jockusch Avatar asked Jun 17 '16 11:06

William Jockusch


People also ask

How do I Use dependency resolution in Xamarin forms?

Injecting a dependency resolution method The DependencyResolver class provides the ability to inject a dependency resolution method into Xamarin.Forms, using the ResolveUsing method. Then, when Xamarin.Forms needs an instance of a particular type, the dependency resolution method is given the opportunity to provide the instance.

How to detect the device size in Xamarin?

2. Create a method to detect the device size To detect the device size use Xamarin Essentials which makes it really easy to get the metrics of a device with DeviceDisplay.MainDisplayInfo.

What is Xamarin control over the lifetime of a type?

Control over the creation and lifetime of types in platform projects is traditionally performed by Xamarin.Forms, which uses the Activator.CreateInstance method to create instances of custom renderers, effects, and DependencyService implementations.

Why Splat for cross-platform mobile?

Certain types of things are basically impossible to do in cross-platform mobile code today, yet there's no reason why. Writing a ViewModel that handles loading a gallery of pictures from disk will be completely riddled with #ifdefs and basically unreadable. Splat aims to fix that, by providing a usable leaky abstraction above platform code.


1 Answers

Alright, so the error in question is:

RectOwnerMac.cs(11,12): error CS7069: Reference to type `System.Drawing.RectangleF' claims it is defined assembly `Splat, Version=1.6.2.0, Culture=neutral, PublicKeyToken=null', but it could not be found
RectOwnerMac.cs(11,19): error CS0029: Cannot implicitly convert type `System.Drawing.RectangleF [Xamarin.Mac, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]' to `System.Drawing.RectangleF [Splat, Version=1.6.2.0, Culture=neutral, PublicKeyToken=null]'

Which is really saying "Splat claimed to have a RectangleF declared, but I can't find it. Oh, and their thing and the RectangleF doesn't match the one I can find.

Which if you look at the source code, is completely true. In Splat-portable they declare their own RectangleF class, but the "bait-and-switch" Splat-XamarinMac does not have one, nor a type forwarder.

You can fix this by adding TypeForwardedSystemDrawing.cs to the Split-XamarinMac project, rebuilding (and commenting out or fixing the UIKit compile error).

Feel free to open an issue with the Splat team to fix this on their end.

Do note, if you try to port Splat to the XM 4.5 target framework, you'll need to pull in OpenTK, because for various legacy reasons the SD types are defined there:

$ monop -r:/Library/Frameworks/Xamarin.Mac.framework/Versions/Current/lib/mono/4.5/Xamarin.Mac.dll | grep Drawing.Rectangle
$ monop -r:/Library/Frameworks/Xamarin.Mac.framework/Versions/Current/lib/mono/4.5/OpenTK.dll | grep Drawing.Rectangle
System.Drawing.Rectangle
System.Drawing.RectangleF
like image 55
Chris Hamons Avatar answered Oct 06 '22 07:10

Chris Hamons