gpt4 book ai didi

c# - Windows Phone 字符串颜色

转载 作者:太空宇宙 更新时间:2023-11-03 21:46:22 25 4
gpt4 key购买 nike

Windows Phone(或 Silverlight)中没有 System.Windows.Media.ColorConverter,所以我需要另一种方法来获取包含颜色名称的字符串,例如“红色”并从中生成一个 Color 对象。

我发现了这种可能性,但它不起作用,因为 colorType.GetProperty 总是返回 null。

public static Color ConvertFromString(string colorString)
{
Color retval = Colors.Transparent;

Type colorType = (typeof(Colors));

if (colorType.GetProperty(colorString) != null)
{
object o = colorType.InvokeMember(colorString,
BindingFlags.GetProperty, null, null, null);

if (o != null)
{
retval = (Color)o;
}
}

return retval;
}

有什么想法吗?

最佳答案

试试这个:

public static Color GetColor(String ColorName)
{
Type colors = typeof(System.Windows.Media.Colors);
foreach(var prop in colors.GetProperties())
{
if(prop.Name == ColorName)
return ((System.Windows.Media.Color)prop.GetValue(null, null));
}

throw new Exception("No color found");
}

关于c# - Windows Phone 字符串颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16823087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com