2014-01-18 97 views
1

我正在尝试创建一个WP8应用程序,我可以使用3个滑块(如RGB一个)动态更改矩形的颜色。当然,我得在后面的代码中的一些其他的东西,但这里是奇怪的事情:WP8 + Color in XAML

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

当我尝试在任何Windows Phone应用程序启动这个代码,在App推出,但有一个XamlParseException其告诉我,我无法将“255”转换为System.Byte。

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

非常感谢!

杜仲

回答

1

你可以绑定你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); 
3

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

Silverlight中的XAML解析器只知道如何处理double,int和bools。 [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