How to fake a library (GDI to Xamarin.Forms) across namespaces and platforms such as reusing Color struct

社会主义新天地 提交于 2019-12-25 09:24:00

问题


Im attempting to reuse legacy Windows GDI code in Xamarin without modifying the legacy GDI code (except using #if). In this specific case I need to use Color from Xamrin.Forms wrapped in my own color struct. I get the error cannot convert as posted in the question header above.

This is the legacy code that is not to be modified (or very little)

#if WINDOWS
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Windows.Forms;
#else //if Xamarin
using FakeGDI;  //cannot specify 'using Xamarin.Forms;' in legacy class

#endif
namespace LegacyCode
{
    public class MyClass
    {
        public MyClass ()
        {
            Color x = Color.Yellow;  //must not change legacy code
        }
    }
}

This is the code to adapt the legacy GDI calls to Xamarin.Forms

using System;
//cannot specify 'using Xamarin.Forms;' I want to fully qualify Xamarin.Forms as shown below 'Xamarin.Forms.Color.Yellow;'

namespace FakeGDI
{
    public struct Color
    {

        public static Color Yellow 
        {
            get { 
                return Xamarin.Forms.Color.Yellow;
                ;}
        }
    }
}

The solution I'm looking for is to get this to compile and use the Xamarin.Forms Color struct via my Color struct indirectly.

What is needed to allow this type of conversion?

The error I get is: Cannot implicitly convert type Xamarin.Forms.Color' toFakeGDI.Color'

Thanks


回答1:


It's absolutely not possible to do it in C#.

You're trying to do:

StructA a;
StructB b;
a = b;

In can never work. It would only be possible if Color were a class, then you could use inheritance.

The constraint that you cannot specify 'using Xamarin.Forms;' in legacy class doesn't leave you any way to do it, because in contrast to languages like JavaScript you can't copy a reference to a class, but only to an object.

However, a solution might be writing the FakeGDI in a different language which does allow copying a reference to a class.

Following is an example how to do it in JavaScript, although you can't really do it in JavaScript, you have to look for a dynamic language which can save its modules as DLLs.

module FakeGDI{
  var Xamarin = require('Xamarin.Forms.Color.Yellow');

  module.exports = {
      Color : Xamarin.Forms.Color
  };
}



回答2:


Your code needs to define the member static properties to be of the correct type. In other words:

namespace FakeGDI
{
    public struct Color
    {
        public static Xamarin.Forms.Color Yellow 
        {
            get { 
                return Xamarin.Forms.Color.Yellow;
            }
        }
    }
}

Note that the Xamarin.Forms namespace is applied to the static property itself.




回答3:


The exception you are seeing is because the return type of your static property FakeGDI.Color.Yellow is FakeGDI.Color but you return an instance of Xamarin.Forms.Color.

Firstly you want your static properties to return an instance of your Color type since this is the Color type you want your legacy code to reference. You then want to define an implicit opertator that will cast your Color type to the Xamarin Color type wherever your legacy code is expecting a Xamarin.Forms.Color but it supplied with your FakeGCI.Color.

You can make your Color type a wrapper for the Xamarin Color type as follows:

namespace FakeGDI
{
    public struct Color 
    {
        private Xamarin.Forms.Color xamarinColor;

        public static Color Yellow
        {
            get
            {
                return new Color(Xamarin.Forms.Color.Yellow);
            }
        }

        private Color(Xamarin.Forms.Color color)
        {
            this.xamarinColor = color;
        }

        public static implicit operator Xamarin.Forms.Color(Color color)
        {
            return color.xamarinColor;
        }

        // although serving no purpose in your snippet, your legacy code will
        // likely need to cast from Xamarin Color to your Color type also
        public static implicit operator Color(Xamarin.Forms.Color color)
        {
            return new Color(color);
        }
    }
}


来源:https://stackoverflow.com/questions/37505910/cannot-implicitly-convert-type-xamarin-forms-color-to-fakegdi-color-with-spe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!