我正在尝试创建一个类,该类保存我们为应用程序做出的各种图形决策的相关数据,并能够在一个位置进行这些更改。然而,每当我调用该类时,我总是得到相同的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]);
}
}
这是因为UIColor
构造函数接受四(4)个System.Single
(float
)值,这些值需要介于0.0f和1.0f之间-所有值都将被视为1.0f。
如果要使用byte
或int
,请使用静态FromRGBA
辅助方法。