C# WPF 令牌在 BrushConverter 上无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17635073/
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
C# WPF Token is not valid on BrushConverter
提问by John
I am converting a System.Media.Brush to a System.Drawing.Brush, but after I change the color. It throws a "Token is not valid" error on the converter.
我正在将 System.Media.Brush 转换为 System.Drawing.Brush,但在更改颜色之后。它会在转换器上引发“令牌无效”错误。
private Brush DrawingColorToBrush(System.Drawing.Color color)
{
Brush ret;
BrushConverter m;
m = new BrushConverter();
ret = (Brush)m.ConvertFromString(color.ToArgb().ToString("X8"));
return ret;
}
The color is coming from a System.Windows.Forms.ColorDialog

颜色来自 System.Windows.Forms.ColorDialog

回答by Gayot Fow
Your code will work if you change your method to this...
如果您将方法更改为此,您的代码将起作用......
private Brush DrawingColorToBrush(System.Drawing.Color color)
{
Brush ret = null;
BrushConverter m = new BrushConverter();
string s = "#" + color.ToArgb().ToString("X8");
if (m.CanConvertFrom(typeof (string)))
{
ret = (Brush) m.ConvertFromString(s);
}
return ret;
}
The key is to prepend the string with the '#' character.
关键是在字符串前加上“#”字符。

