Search code examples
windows-phone-7.1.1

Close open popup on backkey press in Windows phone


In my application I am using a popup in a page. When I press on back key, I need to close the popup if it is open and stay on the page. When I pressed on the back key, the popup not closed and the page goes to previous one. To close the popup now I use the following code in the back key press event.

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
    if (myPopup.IsOpen)
        myPopup.IsOpen = false;

    else if (NavigationService.CanGoBack)
        NavigationService.GoBack();
}

When using this code, the popup will close but also goes to the previous page. I need to go back only if popup is in closed state. How can I do this?


Solution

  • Problem Solved.

    Instead of using

    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
    {
    }
    

    I used the back key press event of the page

    private void PhoneApplicationPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if (myPopup.IsOpen)
            myPopup.IsOpen = false;
    
        else if (NavigationService.CanGoBack)
            NavigationService.GoBack();
    }