dimanche 29 mai 2016

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

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?

Thanks

Aucun commentaire:

Enregistrer un commentaire