2012-09-21 111 views
0

似乎ConvertFromString從WinRT中消失了。所以我很難找到一種方法來獲取組合框中的字符串,並使用它設置文本前景和網格背景。如何將字符串顏色轉換爲WinRT中的畫筆

這裏是我的最新嘗試

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; 
} 

任何建議/解決方法?

回答

1

庫中沒有提供任何轉換。但是,轉換例程很容易編寫,例如http://blog.lookitskris.com/?p=22中提到的轉換例程。

+0

很好,因爲我的comboBox顏色值是已知的,所以我可以使用它。謝謝! –

6

對於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; 
     } 
    } 
0

的ColorConverter似乎WinRT中失蹤,但與反思的一點點,很容易寫自己的。在下面的例子中,我創建了一些擴展方法,使人們有可能寫這樣的代碼:其編譯爲雙方的WinRT和WPF

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

Background = colorName.CreateColorBrush(); 

擴展實現:

#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; 
     } 
    } 
} 
相關問題