wpf 如何将像“Red”这样的字符串转换为 System.Windows.Media.Color?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/672991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I convert a string like "Red" to a System.Windows.Media.Color?
提问by willem
I know I can go the long route by...
我知道我可以走很长的路...
- Adding a reference to System.Drawing
- Creating a System.Drawing.Color from the string
- Creating the System.Windows.Media.Color from the ARGB values of the System.Drawing.Color.
- 添加对 System.Drawing 的引用
- 从字符串创建 System.Drawing.Color
- 从 System.Drawing.Color 的 ARGB 值创建 System.Windows.Media.Color。
But this feels like serious overkill.
但这感觉像是严重的矫枉过正。
Is there an easier way?
有更容易的方法吗?
回答by Kent Boogaart
var color = (Color)ColorConverter.ConvertFromString("Red");
回答by Jon Skeet
New and better answer
新的更好的答案
Of course, ColorConverter is the way to go. Call ColorConverter.ConvertFromStringand cast the result. Admittedly this will involve boxing. If you want to avoid boxing, build a dictionary up to start with for the standard names (still using ColorConverter) and then use the dictionary for subsequent lookups.
当然,ColorConverter 是必经之路。调用ColorConverter.ConvertFromString并转换结果。诚然,这将涉及拳击。如果您想避免装箱,请为标准名称构建一个字典(仍然使用 ColorConverter),然后使用该字典进行后续查找。
Original answer
原答案
You could fairly easily fetch the property names and values from System.Windows.Media.Colorsonce into a map:
您可以很容易地从System.Windows.Media.Colors获取属性名称和值一次到地图中:
private static readonly Dictionary<string, Color> KnownColors = FetchColors();
public static Color FromName(string name)
{
return KnownColors[name];
}
private static Dictionary<string, Color> FetchColors()
{
// This could be simplified with LINQ.
Dictionary<string, Color> ret = new Dictionary<string, Color>();
foreach (PropertyInfo property in typeof(Colors).GetProperties())
{
ret[property.Name] = (Color) property.GetValue(null);
}
return ret;
}
It's a bit ugly, but it's a one-time hit.
这有点难看,但它是一次性的。
回答by Jon Skeet
System.Windows.Media.ColorConverteris how the XamlReader does it.
System.Windows.Media.ColorConverter是 XamlReader 的工作方式。
var result = ColorConverter.ConvertFromString("Red") as Color;
回答by Tomasz Smykowski
This code makes translating name to Color
class faster:
此代码可以Color
更快地将名称转换为类:
public class FastNameToColor
{
Dictionary<string, Color> Data = new Dictionary<string, Color>();
public FastNameToColor()
{
System.Reflection.PropertyInfo[] lColors = typeof(System.Drawing.Color).GetProperties();
foreach (PropertyInfo pi in lColors)
{
object val = pi.GetValue(null, null);
if (val is Color)
{
Data.Add(pi.Name, (Color)val);
}
}
}
public Color GetColor(string Name)
{
return Data[Name];
}
}
You can expand this code to translate name to Media.Color
directly.
您可以扩展此代码以Media.Color
直接将名称转换为。
回答by kirie
Fyi, another easier way is just use microsoft built static class, ex Colors.Red
仅供参考,另一种更简单的方法是使用微软构建的静态类,例如 Colors.Red
http://msdn.microsoft.com/en-us/library/windows/desktop/bb189018.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/bb189018.aspx