0

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

提案/回避策はありますか?

4

3 に答える 3

1

ライブラリで提供される変換はありません。ただし、変換ルーチンは、 http://blog.lookitskris.com/?p = 22で説明されているように、簡単に作成できます。

于 2012-09-21T02:09:37.837 に答える
0

WinRT には ColorConverter がないように見えますが、少しリフレクションを使用すると、独自のものを簡単に作成できます。以下の例では、次のようなコードを記述できるようにするいくつかの拡張メソッドを作成しました。

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;
        }
    }
}
于 2014-11-13T10:27:17.563 に答える