Silverlight 为什么WP7中的按钮行为不同
两个按钮:Silverlight 为什么WP7中的按钮行为不同,silverlight,windows-phone-7,Silverlight,Windows Phone 7,两个按钮: <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" Marg
<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" />
在一个普通的Silverlight应用程序中,一切都完全按照预期工作。但是,在Windows Phone 7中,完全相同的代码的行为如下所示:
按钮1不改变颜色(只是保持红色)
按钮2会改变颜色,除非我点击它,在这种情况下,当我点击按钮1时,它不再改变颜色(即,它的颜色现在也被卡住)
线索是谁?我会尝试这样设置按钮的背景。这将在按钮线程上运行背景代码(至少据我所知) 我不确定这是否能解决任何问题,但值得一试
-Woody以下几点应该可以做到:
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;
我来过很多次。用视觉状态看,我什么都没有。抱歉,为什么这不起作用,请参见
button2.Dispatcher.BeginInvoke(delegate
{
button2.Background = new SolidColorBrush(c);
});
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;