在 C# 中将字符串转换为画笔/画笔颜色名称

发布于 2024-07-11 04:14:52 字数 421 浏览 6 评论 0原文

我有一个配置文件,开发人员可以通过传入字符串来指定文本颜色:

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

小情绪 2024-07-18 04:14:52

回顾之前的所有答案,将字符串转换为颜色或画笔的不同方法:

// best, using Color's static method
Color red1 = Color.FromName("Red");

// using a ColorConverter
TypeConverter tc1 = TypeDescriptor.GetConverter(typeof(Color)); // ..or..
TypeConverter tc2 = new ColorConverter();
Color red2 = (Color)tc.ConvertFromString("Red");

// using Reflection on Color or Brush
Color red3 = (Color)typeof(Color).GetProperty("Red").GetValue(null, null);

// in WPF you can use a BrushConverter
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");

Recap of all previous answers, different ways to convert a string to a Color or Brush:

// best, using Color's static method
Color red1 = Color.FromName("Red");

// using a ColorConverter
TypeConverter tc1 = TypeDescriptor.GetConverter(typeof(Color)); // ..or..
TypeConverter tc2 = new ColorConverter();
Color red2 = (Color)tc.ConvertFromString("Red");

// using Reflection on Color or Brush
Color red3 = (Color)typeof(Color).GetProperty("Red").GetValue(null, null);

// in WPF you can use a BrushConverter
SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("Red");
又怨 2024-07-18 04:14:52

刷弦:

myTextBlock.Foreground = new BrushConverter().ConvertFromString("#FFFFFF") as SolidColorBrush;

这就是我这里的情况!

String to brush:

myTextBlock.Foreground = new BrushConverter().ConvertFromString("#FFFFFF") as SolidColorBrush;

That's my case here!

北方的巷 2024-07-18 04:14:52

画笔可以这样声明

Brush myBrush = new SolidBrush(Color.FromName("Red"));

A brush can be declared like this

Brush myBrush = new SolidBrush(Color.FromName("Red"));
旧话新听 2024-07-18 04:14:52

噢。 看了一会儿我发现:

 Color.FromName(a.Value)

点击“发布”后。 从那里到:

 color = new SolidBrush(Color.FromName(a.Value));

我将把这个问题留给其他人......

D'oh. After a while of looking I found:

 Color.FromName(a.Value)

After hitting "post". From there it's a short step to:

 color = new SolidBrush(Color.FromName(a.Value));

I'll leave this question here for others....

乱了心跳 2024-07-18 04:14:52

您可以为此使用反射:

Type t = typeof(Brushes);
Brush b = (Brush)t.GetProperty("Red").GetValue(null, null);

当然,如果字符串错误,您将需要一些错误处理/范围检查。

You could use reflection for this:

Type t = typeof(Brushes);
Brush b = (Brush)t.GetProperty("Red").GetValue(null, null);

Of course, you'll want some error handling/range checking if the string is wrong.

楠木可依 2024-07-18 04:14:52

我同意使用 TypeConverters 是最好的方法:

 Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
 return new Brush(c);

I agree that using TypeConverters are the best method:

 Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
 return new Brush(c);
仅冇旳回忆 2024-07-18 04:14:52

如果需要,您可以进一步扩展并允许他们指定 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);

总攻大人 2024-07-18 04:14:52

尝试使用TypeConverter。 示例:

var tc = TypeDescriptor.GetConverter(typeof(Brush));

另一种选择是使用反射,并检查 SystemBrushes 中的属性。

Try using a TypeConverter. Example:

var tc = TypeDescriptor.GetConverter(typeof(Brush));

Another alternative is to use reflection, and go over the properties in SystemBrushes.

季末如歌 2024-07-18 04:14:52

在尝试解决这个问题时,我遇到了命名空间冲突。 结果 BrushColor 类存在于两个不同的命名空间 System.Windows.MediaSystem.Drawing 中,并且它们具有相似的函数名称,但也存在差异。

如果您使用如下所示的控件,它会使用媒体命名空间,因此请确保在使用此对象的任何位置添加了正确的命名空间。

<Button Content="Change Color" Background="AliceBlue"/>

接受的答案中的这一部分确实是正确的(最佳)答案,但了解名称空间是件好事。

Brush color = (SolidColorBrush)new BrushConverter().ConvertFromString("Green");

请注意,以下内容(如接受的答案中所建议的那样)不会在此名称空间中编译。

Color red1 = Color.FromName("Red"); // don't even try, its wrong namespace

因为 System.Windows.Media 中的 Color 类没有 FromName() 成员,因此它甚至不适用。

上面有很多解决方案,但没有人提到名称空间,答案实际上取决于此。 这个问题实际上是关于 Media 命名空间的,因为它有颜色名称,但不提及可能会让尝试这些解决方案的新读者感到困惑。

像我这样的许多人甚至可能不需要转换并使用问题本身中指出的枚举。

Brush color = Brushes.Red;

I was getting namespace conflicts when trying solutions to this question. Turns out Brush and Color classes exists in two difference namespaces System.Windows.Media and System.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.

<Button Content="Change Color" Background="AliceBlue"/>

This part in the accepted answer is really right(best) answer but it's good to know about the namespaces.

Brush color = (SolidColorBrush)new BrushConverter().ConvertFromString("Green");

Do note that the following (as suggested in accepted answer) doesn't compile in this namespace.

Color red1 = Color.FromName("Red"); // don't even try, its wrong namespace

Because the Color class in System.Windows.Media doesn't have FromName() 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.

Brush color = Brushes.Red;
攀登最高峰 2024-07-18 04:14:52

您可以使用 System.Drawing。 KnownColor 枚举。 它指定所有已知的系统颜色。

You can use System.Drawing.KnownColor enum. It specifies all known system colors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文