【问题标题】:WP8 + Color in XAMLXAML 中的 WP8 + 颜色
【发布时间】:2014-01-18 23:57:01
【问题描述】:

我正在尝试创建一个 WP8 应用程序,我可以在其中使用 3 个滑块(如 RGB 滑块)动态更改矩形颜色。当然,我在代码中还有其他一些东西,但奇怪的是:

        <Rectangle Width="100" Height="100">
            <Rectangle.Fill>
                <SolidColorBrush>
                    <SolidColorBrush.Color>
                        <Color A="255" R="255"/>
                    </SolidColorBrush.Color>
                </SolidColorBrush>
            </Rectangle.Fill>
        </Rectangle>

当我尝试在任何 Windows Phone 应用程序中启动此代码时,该应用程序正在启动,但有一个 XamlParseException 告诉我无法将“255”转换为 System.Byte。

最奇怪的是我的代码在 WPF 应用程序中工作...:s 有人有问题吗?

非常感谢!

奥利夫

【问题讨论】:

    标签: wpf xaml windows-phone


    【解决方案1】:

    Silverlight 不知道如何将字符串转换为字节。

    Silverlight 中的 XAML 解析器只知道如何处理双精度、整数和布尔值。 [Reference]

    您可以使用十六进制代替 ARGB:

    <Rectangle Width="100" Height="100">
        <Rectangle.Fill>
            <SolidColorBrush Color="#FFFF0000" />
        </Rectangle.Fill>
    </Rectangle>
    

    A=255, R=255, G=0, B=0 等价于十六进制的A=FF, R=FF, G=00, B=00

    【讨论】:

      【解决方案2】:

      您可以将整个 Rectangle.Fill 绑定到 SolidColorBrush,而不是单个颜色通道。

      <Rectangle Width="100" Height="100" Fill="{Binding FillBrush}" />
      
      private SolidColorBrush fillBrush = new SolidColorBrush(Colors.Transparent);
      public SolidColorBrush FillBrush
      {
         get
         {
            return fillBrush;
         }
         set
         {
            fillBrush = value;
            OnPropertyChanged();
         }
      }
      

      每次滑块值发生变化时,根据这些值创建一个新的 SolidColorBrush。

      Color fillColor = Color.FromArgb((byte)255, (byte)255, (byte)255, (byte)255);
      FillBrush = new SolidColorBrush(fillColor);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-01
        • 2023-04-07
        相关资源
        最近更新 更多