1

我正在尝试创建一个类来保存我们为应用程序做出的各种图形决策的相关数据,并能够在一个位置进行这些更改。但是,每当我调用该类时,我总是得到 UIColor.White 的相同返回,而不是我请求的任何颜色。我在主项目中引用的 Xamarin.iOS 库中确实有这个类。

这是一个代码示例:

public static class MyColors
{
    public static UIColor BackgroundColor
    {
        get { return ConvertHexToColor("fa0000"); }
    }

    public static UIColor ConvertHexToColor(string hex)
    {
        if (hex.Contains('#')) hex.Replace("#", "");
        int[] rgba = new int[] { 255, 255, 255, 255 };

        if (hex.Length == 6)
        {
            rgba[0] = Convert.ToInt32(hex.Substring(0, 2), 16);
            rgba[1] = Convert.ToInt32(hex.Substring(2, 2), 16);
            rgba[2] = Convert.ToInt32(hex.Substring(4, 2), 16);
        }
        else if (hex.Length == 8)
        {
            rgba[0] = Convert.ToInt32(hex.Substring(0, 2), 16);
            rgba[1] = Convert.ToInt32(hex.Substring(2, 2), 16);
            rgba[2] = Convert.ToInt32(hex.Substring(4, 2), 16);
            rgba[3] = Convert.ToInt32(hex.Substring(6, 2), 16);
        }
        else
        {
            throw new ArgumentException("Hash must be color code six or eight characters in length.");
        }

        return new UIColor(rgba[0], rgba[1], rgba[2], rgba[3]);
    } 
}
4

1 回答 1

5

这是因为构造函数接受需要介于 0.0f 和 1.0f 之间的UIColor四 (4) System.Single( ) 个值 - 一切都将被视为 1.0f。float

如果你想使用byte或者int然后使用静态FromRGBA辅助方法。

于 2013-07-15T20:21:00.267 回答