如何将string颜色转换为WinRT中的画笔

看起来像ConvertFromString从WinRT中消失了。 所以我无法find一种方法来获取combobox中的string,并使用它设置文本的前景和网格的背景。

这是我最近的尝试

private void ColorDropBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e) { string backGroundColor = e.ToString(); SolidColorBrush newcolor = new SolidColorBrush(); newcolor = backGroundColor as SolidColorBrush; this.ContentRoot.Background = newcolor; } 

任何build议/解决方法?

图书馆没有提供转换。 但是,转换例程很容易编写,例如http://blog.lookitskris.com/?p=22中提到的转换例程。

对于WinRT

 using Windows.UI; using Windows.UI.Xaml.Media; public static Brush ColorToBrush(string color) { color = color.Replace("#", ""); if (color.Length == 6) { return new SolidColorBrush(ColorHelper.FromArgb(255, byte.Parse(color.Substring(0, 2), System.Globalization.NumberStyles.HexNumber), byte.Parse(color.Substring(2, 2), System.Globalization.NumberStyles.HexNumber), byte.Parse(color.Substring(4, 2), System.Globalization.NumberStyles.HexNumber))); } else { return null; } } 

ColorConverter似乎在WinRT中不见了,但是有一点反思,很容易写出自己的。 在下面的例子中,我创建了一些扩展方法,可以编写如下代码:

 Color red = "Red".ConvertToColor(); or Color color = colorName.ConvertToColor(); 

 Background = colorName.CreateColorBrush(); 

为WinRT和WPF编译的扩展实现:

 #if NETFX_CORE using Windows.UI; using Windows.UI.Xaml.Media; #else using System.Windows.Media; #endif using System; using System.Globalization; using System.Reflection; namespace YourNiceExtensionsNamespace { /// <summary> /// Extension to convert a string color name like "Red", "red" or "RED" into a Color. /// Using ColorsConverter instead of ColorConverter as class name to prevent conflicts with /// the WPF System.Windows.Media.ColorConverter. /// </summary> public static class ColorsConverter { /// <summary> /// Convert a string color name like "Red", "red" or "RED" into a Color. /// </summary> public static Color ConvertToColor(this string colorName) { if (string.IsNullOrEmpty(colorName)) throw new ArgumentNullException("colorName"); MethodBase getColorMethod = FindGetColorMethod(colorName); if (getColorMethod == null) { // Using FormatException like the WPF System.Windows.Media.ColorConverter throw new FormatException(string.Format(CultureInfo.InvariantCulture, "Color name {0} not found in {1}", colorName, typeof(Colors).FullName)); } return (Color)getColorMethod.Invoke(null, null); } /// <summary> /// Create a SolidColorBrush from a color name /// </summary> public static Brush CreateColorBrush(this string colorName) { if (string.IsNullOrEmpty(colorName)) throw new ArgumentNullException("colorName"); Color color = colorName.ConvertToColor(); return new SolidColorBrush(color); } /// <summary> /// Verify if a string color name like "Red", "red" or "RED" is a known color in the static Colors class /// </summary> public static bool IsColorName(this string colorName) { if (string.IsNullOrEmpty(colorName)) throw new ArgumentNullException("colorName"); return FindGetColorMethod(colorName) != null; } private static MethodBase FindGetColorMethod(string colorName) { foreach (PropertyInfo propertyInfo in typeof(Colors).GetTypeInfo().DeclaredProperties) { if (propertyInfo.Name.Equals(colorName, StringComparison.OrdinalIgnoreCase)) { MethodBase getMethod = propertyInfo.GetMethod; if (getMethod.IsPublic && getMethod.IsStatic) return getMethod; break; } } return null; } } }