我正在尝试创建一个类来保存我们为应用程序做出的各种图形决策的相关数据,并能够在一个位置进行这些更改。但是,每当我调用该类时,我总是得到 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]);
}
}