两个按钮这个按钮行为:为什么在WP7不同
<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应用程序如预期寄托都工作的。然而,在Windows Phone 7中,完全相同的代码行为如下:
button1不会改变颜色(它只是保持红色) button2确实会改变颜色,除非我点击它,在这种情况下,当我点击button1时它不再改变颜色(即它的颜色现在也卡住了)
线索enyone?
在这里多次。使用视觉状态http://stackoverflow.com/questions/3476963/windows-phone-7-wp7-change-a-buttons-background-color-on-click –
请参阅http://blogs.msdn.com/b/ ptorr/archive/2010/06/18/why-can-ti-change-the-background-of-my-button-on-a-click-event.aspx –