How do I convert a string color to a Brush in WinR

2019-08-23 16:12发布

问题:

It seems like ConvertFromString is gone from WinRT. So I am having trouble finding a way to take a string in a combobox and using it set text foreground and a grid's background.

Here is my latest attempt

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

Any suggestions/workarounds?

回答1:

There is no conversion supplied in the library. However, conversion routines are simple to write, such as the one mentioned in http://blog.lookitskris.com/?p=22.



回答2:

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


回答3:

The ColorConverter seems to be missing in WinRT, but with a little bit of reflection it is easy to write your own. In the example below I created some extension methods, which make it possible to write code like:

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

and

Background = colorName.CreateColorBrush();

Extension implementation which compiles for both WinRT and 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;
        }
    }
}