在 C# 中将字符串转换为画笔/画笔颜色名称
我有一个配置文件,开发人员可以通过传入字符串来指定文本颜色:
<text value="Hello, World" color="Red"/>
与其使用巨大的 switch 语句查找所有可能的颜色,不如只使用 System.Drawing 类中的属性.Brushes 相反,因此在内部我可以这样说:
Brush color = Brushes.Black; // Default
// later on...
this.color = (Brush)Enum.Parse(typeof(Brush), prasedValue("color"));
除了 Brush/Brushes 中的值不是枚举。 所以 Enum.Parse 没有给我带来任何快乐。 建议?
I have a configuration file where a developer can specify a text color by passing in a string:
<text value="Hello, World" color="Red"/>
Rather than have a gigantic switch statement look for all of the possible colors, it'd be nice to just use the properties in the class System.Drawing.Brushes instead so internally I can say something like:
Brush color = Brushes.Black; // Default
// later on...
this.color = (Brush)Enum.Parse(typeof(Brush), prasedValue("color"));
Except that the values in Brush/Brushes aren't enums. So Enum.Parse gives me no joy. Suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
回顾之前的所有答案,将字符串转换为颜色或画笔的不同方法:
Recap of all previous answers, different ways to convert a string to a Color or Brush:
刷弦:
这就是我这里的情况!
String to brush:
That's my case here!
画笔可以这样声明
A brush can be declared like this
噢。 看了一会儿我发现:
点击“发布”后。 从那里到:
我将把这个问题留给其他人......
D'oh. After a while of looking I found:
After hitting "post". From there it's a short step to:
I'll leave this question here for others....
您可以为此使用反射:
当然,如果字符串错误,您将需要一些错误处理/范围检查。
You could use reflection for this:
Of course, you'll want some error handling/range checking if the string is wrong.
我同意使用 TypeConverters 是最好的方法:
I agree that using TypeConverters are the best method:
如果需要,您可以进一步扩展并允许他们指定 R、G 和 B 值。 然后你只需调用 Color.FromArgb(int r, int g, int b);
If you want, you can extend this even more and allow them to specify values for the R, G and B values. Then you just call Color.FromArgb(int r, int g, int b);
尝试使用
TypeConverter
。 示例:另一种选择是使用反射,并检查
SystemBrushes
中的属性。Try using a
TypeConverter
. Example:Another alternative is to use reflection, and go over the properties in
SystemBrushes
.在尝试解决这个问题时,我遇到了命名空间冲突。 结果
Brush
和Color
类存在于两个不同的命名空间System.Windows.Media
和System.Drawing
中,并且它们具有相似的函数名称,但也存在差异。如果您使用如下所示的控件,它会使用媒体命名空间,因此请确保在使用此对象的任何位置添加了正确的命名空间。
接受的答案中的这一部分确实是正确的(最佳)答案,但了解名称空间是件好事。
请注意,以下内容(如接受的答案中所建议的那样)不会在此名称空间中编译。
因为
System.Windows.Media
中的Color
类没有FromName()
成员,因此它甚至不适用。上面有很多解决方案,但没有人提到名称空间,答案实际上取决于此。 这个问题实际上是关于 Media 命名空间的,因为它有颜色名称,但不提及可能会让尝试这些解决方案的新读者感到困惑。
像我这样的许多人甚至可能不需要转换并使用问题本身中指出的枚举。
I was getting namespace conflicts when trying solutions to this question. Turns out
Brush
andColor
classes exists in two difference namespacesSystem.Windows.Media
andSystem.Drawing
and they have similar function names but there are differences too.If you working with controls like below, it uses Media namespace so make sure you have the correct namespace added wherever you use this objects.
This part in the accepted answer is really right(best) answer but it's good to know about the namespaces.
Do note that the following (as suggested in accepted answer) doesn't compile in this namespace.
Because the
Color
class inSystem.Windows.Media
doesn't haveFromName()
member so it's not even applicable.So so many solutions above but nobody has mentioned namespaces and the answer really depends on that. This question is actually about the Media namespace because that has color names but not mentioning that can confuse new readers who is trying these solutions.
Many people like myself may even not even need the conversion and use the enum as noted in the question itself.
您可以使用 System.Drawing。 KnownColor 枚举。 它指定所有已知的系统颜色。
You can use System.Drawing.KnownColor enum. It specifies all known system colors.