两个按钮:
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
<Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="10,92,0,0" Name="button2" VerticalAlignment="Top" Width="160" />
自解释代码:
public MainPage()
{
InitializeComponent();
button1.Background = new SolidColorBrush(Colors.Red);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Color c = ((SolidColorBrush)((Button)sender).Background).Color;
if (c == Colors.Red)
{
c = Colors.Green;
}
else if (c == Colors.Green)
{
c = Colors.Blue;
}
else if (c == Colors.Blue)
{
c = Colors.Red;
}
else
{
c = Colors.Yellow;
}
((Button)sender).Background = new SolidColorBrush(c);
button2.Background = new SolidColorBrush(c);
}
在普通的Silverlight应用程序中,everthing的工作方式完全符合预期。但是,在Windows Phone 7中,完全相同的代码表现如下:
button1不会改变颜色(它只是保持红色) button2确实改变颜色,除非我点击它,在这种情况下,当我点击button1时它不再改变颜色(即它的颜色现在也被卡住了)
线索enyone?
答案 0 :(得分:1)
以下应该做的诀窍:
var btn = sender as Button;
var brush = ((SolidColorBrush)btn.Background);
if (brush.Color == Colors.Red)
brush.Color = Colors.Green;
else if (brush.Color == Colors.Green)
brush.Color = Colors.Blue;
else if (brush.Color == Colors.Blue)
brush.Color = Colors.Red;
else
brush.Color = Colors.Yellow;
btn.Background = brush;
button2.Background = brush;
答案 1 :(得分:0)
我会尝试这样的设置Button的背景。这将在Button的主题上运行后台代码(至少我的理解)
button2.Dispatcher.BeginInvoke(delegate
{
button2.Background = new SolidColorBrush(c);
});
我不确定这是否会解决问题,但值得一试。
-Woody