Silverlight Windows Phone 7 - Button click event lost after using hardware back button
On a Windows phone 7 Silverlight project, when collapsing a panel containing a button, and then using the hardware back button to return (setting again visibility to visible), the FIRST click event is lost.
This has been driving me crazy for a while.
Here is an easy way to reproduce, start a new Wp7 silverlight app, and on xaml add two content panels with a button.
<Grid x:Name="ContentPanel" Grid.Row="1" Visibility="Visible">
<Button Content="Change Visibility" Click="ChangeButton_Click"/>
</Grid>
<Grid x:Name="ContentPanel2" Grid.Row="1" Visibility="Collapsed">
<Button Content="Back" Click="BackButton_Click"/>
</Grid>
The code behind has handlers for both buttons and for the hardware back button to simply switch the visibility between the two panels.
public MainPage()
{
InitializeComponent();
BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(MainPage_BackKeyPress);
}
void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
if (ContentPanel2.Visibility == Visibility.Visible)
{
ContentPanel.Visibility = Visibility.Visible;
ContentPanel2.Visibility = Visibility.Collapsed;
e.Cancel = true;
}
}
private void ChangeButton_Click(object sender, RoutedEventArgs e)
{
ContentPanel.Visibility = Visibility.Collapsed;
ContentPanel2.Visibility = Visibility.Visible;
}
private void BackButton_Click(object sender, RoutedEventArgs e)
{
ContentPanel.Visibility = Visibility.Visible;
ContentPanel2.Visibility = Visibility.Collapsed;
}
And now the weird thing:
- If you use the on-screen back button switching works开发者_StackOverflow fine
- If you use the hardware back button to switch back, then the FIRST click event afterwards will be lost!
This happens both on emulator and on a real device.
Is this a bug? or what am I doing wrong?
Someone called Fendors (Thanks!) replied to my question on the app hub forums. He didn't find the reason why it happened, but found a workaround:
"I didn't figure out fully what is happening when the hardware back button is clicked, but did find a workaround. I was initially thinking maybe there was some sort of focus issue and found that if you set the content panel from visible to collapsed to visible, it puts it into a good state"
void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
if (ContentPanel2.Visibility == Visibility.Visible)
{
ContentPanel2.Visibility = Visibility.Collapsed;
ContentPanel.Visibility = Visibility.Visible;
ContentPanel.Visibility = Visibility.Collapsed;
ContentPanel.Visibility = Visibility.Visible;
e.Cancel = true;
}
}
Ran into this same problem. Seems like a bug (great find on the workaround). I wrote this simple extension method for the workaround:
public static class UIElementExtensions
{
public static void ShowForReal(this UIElement element)
{
element.Visibility = Visibility.Visible;
element.Visibility = Visibility.Collapsed;
element.Visibility = Visibility.Visible;
}
}
then just call ContentPanel.ShowForReal();
精彩评论