简体   繁体   English

XAML中的WP8 +颜色

[英]WP8 + Color in XAML

I'm trying to create a WP8 application in which I can dynamically change a Rectangle color with 3 Sliders (like RGB one). 我正在尝试创建WP8应用程序,在其中可以使用3个Sliders(例如RGB之一)动态更改Rectangle颜色。 Of course, I've got some other stuff in code behind but here is the weird thing : 当然,我在代码中还有其他一些东西,但这是很奇怪的事情:

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

When I try to launch this code in any Windows Phone Application, the App is launching but there is a XamlParseException which tells me that I cannot convert "255" to System.Byte. 当我尝试在任何Windows Phone应用程序中启动此代码时,该应用程序正在启动,但是存在XamlParseException,它告诉我无法将“ 255”转换为System.Byte。

The srangest thing is that my code works in a WPF application... :s Does anybody has an issue ? 最奇怪的是我的代码在WPF应用程序中可以工作...:s有人遇到问题吗?

Thank you very much ! 非常感谢你 !

Oliv 奥利夫

Silverlight doesn't know how to convert string to byte. Silverlight不知道如何将字符串转换为字节。

The XAML parser in Silverlight only knows how to handle doubles, ints and bools. Silverlight中的XAML解析器仅知道如何处理double,int和bool。 [ Reference ] [ 参考 ]

You can use hexadecimal instead of ARGB : 您可以使用十六进制代替ARGB:

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

A=255, R=255, G=0, B=0 equivalent to Hexadecimal A=FF, R=FF, G=00, B=00 . A=255, R=255, G=0, B=0等于十六进制A=FF, R=FF, G=00, B=00

You can bind your Rectangle.Fill as a whole to a SolidColorBrush, instead of individual color channels. 您可以将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();
   }
}

Every time your slider values change, create a new SolidColorBrush based on those values. 每次滑块值更改时,都基于这些值创建一个新的SolidColorBrush。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM