Nelson T Joseph
Nelson T Joseph

Reputation: 2763

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?

Upvotes: 1

Views: 953

Answers (2)

uWat
uWat

Reputation: 147

You can also do the following:

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

Upvotes: 1

Nelson T Joseph
Nelson T Joseph

Reputation: 2763

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();
}

Upvotes: 0

Related Questions