Using Popups in your application is a great way to present the user a nice “Yes/No” question, or provide some sort of temporary content. However, it does provide an additional challenge to handling the device’s “Back” button. According to Microsoft guidelines when the user presses the “Back” button the app should “Ensure the application closes the current screen in focus, and returns the user to the previous page.” In other words, pressing the “Back” button, should just close your popup window and not the whole page. Failure to implement this has resulted in our apps failing the approval process of 5.2 Performance and Resource Management of section 5.2.4.b. In next section technical detail of certification failure is described.
5.2.4.b Performance and Resource Management Requirements
Pressing the Back button must return the application to the previous page.
Expected Result
Test Process Required:
1. Launch the application.
2. Navigate through the application.
3. Select the device Back Button.
4. Ensure the application closes the current screen in focus, and returns the user to the previous page.
5. Pressing the device back button may alternatively show a menu or a dialog box that the application surfaces.
Steps to reproduce:
1. Launch the application.
2. Select the author’s name and then select “Bio”.
3. Press the back button on the device.
4. Notice the application does not return to the previous page and it terminates after pressing the back button twice.
5. This error is reproducible 10 out of 10 times.
Using OnBackKeyPress To Handle Popup
To overcome 5.2.4.b you would need to override the OnBackKeyPress event as shown in below code. First check if your Popup IsOpen and set it to false if open and then set e.Cancel to true so you can cancel the back button.
protected override void OnBackKeyPress
(System.ComponentModel.CancelEventArgs e)
{
//Check if the PopUp window is open
if (MyPopupWindow.IsOpen)
{
//Close the PopUp Window
MyPopupWindow.IsOpen = false;
//Keep the back button from
//navigating away from the current page
e.Cancel = true;
}
else
{
//There is no PopUp open, use the back button normally
base.OnBackKeyPress(e);
}
}
Conclusion
You learn about how to handle Popup windows when the user clicks the back button on the phone and avoid the Marketplace certification failure.


Pingback: How to create Reusable About Page and learn about NavigationService, WebBrowserTask, MarketplaceDetailTask, and Page Transition with Animation? | Toetapz's Blog