MAUI 图形颜色到原生 Android/iOS 颜色

Fre*_*Ali 6 c# maui .net-6.0 .net-maui

当我意识到没有直接的帮助器方法可以手动找到执行 toMaui.Graphics.ColorAndroid.Graphics.Colora 的转换器时,我正在创建一个自定义控件UIKIT.UIColor

我错过了什么吗?在 XF 中我们有类似的方法

XFColor.ToUIColor();
XFColor.ToAndroidColor();
Run Code Online (Sandbox Code Playgroud)

如果我遗漏了什么,请告诉我

Fre*_*Ali 9

更新:

您现在可以在以下命名空间中找到一个扩展方法来执行此操作。

using Microsoft.Maui.Platform;
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它:

color.ToPlatform(); // here color is a Maui.Graphics.Color
Run Code Online (Sandbox Code Playgroud)

奥格回答:

好吧,我找不到直接的辅助方法/类来执行此操作,因此我只是创建了一些扩展方法来执行此操作:

#if ANDROID
using NativeColor = Android.Graphics.Color;
#endif
#if IOS
using NativeColor = UIKit.UIColor;
#endif

public static class Extensions
    {

    /// <summary>
        /// Get native color from maui color 
        /// </summary>
        /// <param name="color"></param>
        /// <returns>native Color</returns>
        public static NativeColor ToNativeColor(this Color color)
        {
            var hexCode = color.ToHex();
            NativeColor nativeColor;
#if ANDROID
            nativeColor=  Android.Graphics.Color.ParseColor(hexCode);
#endif
#if IOS
            nativeColor = UIKit.UIColor.Clear.FromHex(hexCode);
#endif
            return nativeColor;
        }

#if IOS
        public static UIColor FromHex(this UIColor color, string hex)
        {
            int r = 0, g = 0, b = 0, a = 0;

            if (hex.Contains("#"))
                hex = hex.Replace("#", "");

            switch (hex.Length)
            {
                case 2:
                    r = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
                    g = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
                    b = int.Parse(hex, System.Globalization.NumberStyles.AllowHexSpecifier);
                    a = 255;
                    break;
                case 3:
                    r = int.Parse(hex.Substring(0, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                    g = int.Parse(hex.Substring(1, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                    b = int.Parse(hex.Substring(2, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                    a = 255;
                    break;
                case 4:
                    r = int.Parse(hex.Substring(0, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                    g = int.Parse(hex.Substring(1, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                    b = int.Parse(hex.Substring(2, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                    a = int.Parse(hex.Substring(3, 1), System.Globalization.NumberStyles.AllowHexSpecifier);
                    break;
                case 6:
                    r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    a = 255;
                    break;
                case 8:
                    r = int.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    g = int.Parse(hex.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    b = int.Parse(hex.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    a = int.Parse(hex.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
                    break;
            }

            return UIColor.FromRGBA(r, g, b, a);
        }
#endif

    }
Run Code Online (Sandbox Code Playgroud)

我希望这对某人有帮助,不确定这是否是最好的解决方案,所以如果您有更好的解决方案,请告诉我,以便我可以更新:)