简体   繁体   中英

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). 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.

The srangest thing is that my code works in a WPF application... :s Does anybody has an issue ?

Thank you very much !

Oliv

Silverlight doesn't know how to convert string to byte.

The XAML parser in Silverlight only knows how to handle doubles, ints and bools. [ Reference ]

You can use hexadecimal instead of 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 .

You can bind your Rectangle.Fill as a whole to a SolidColorBrush, instead of individual color channels.

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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