兩個按鈕這個按鈕行爲:爲什麼在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 –