Ashwin Nagarajan
Ashwin Nagarajan

Reputation: 219

Windows Phone Back KeyPress + MessageBox crashes the app without Selection

I have a strange issue overriding BackkeyPress Function in code behind, inside the function i have a simple message box to Go back or cancel navigation ( stay in current page ), when no choice is made (ok or cancel ) and Messagebox is open for long time, Application crashes, when i try to debug, no exception is thrown and App remains in the state unless OK or cancel is pressed , but on Normal run ( without debugger ) the crash is apparent.

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

        string caption = "exit?";
        string message = "Do you still want to exit?";
        e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption,           
        MessageBoxButton.OKCancel);

        base.OnBackKeyPress(e);

    }

Upvotes: 2

Views: 2238

Answers (3)

user3621697
user3621697

Reputation: 19

When using Terminate() - be aware that a number of app.xaml.cs rootFrame navigating events associated with normal exit won't trigger, neither the ApplicationClosing or your page's OnNavigatedFrom. So check if anything going on is important. You might tack it on before terminating...

Upvotes: -1

Rishabh876
Rishabh876

Reputation: 3180

I found one more solution to this, so I thought it would be good if I posted it here. It's just a workaround though.

private async void PhoneApplicationPage_BackKeyPress (object sender, System.ComponentModel.CancelEventArgs e)
{
     e.Cancel = true;
     await Task.Delay(100);
     if (MessageBox.Show(msg, cap, MessageBoxButton.OKCancel) == MssageBoxResult.OK)
     {
          //somecode
     }                
}

Source

Upvotes: 0

Vladimir Gondarev
Vladimir Gondarev

Reputation: 1243

http://msdn.microsoft.com/en-US/library/windowsphone/develop/jj206947(v=vs.105).aspx

In Windows Phone 8, if you call Show in OnBackKeyPress(CancelEventArgs) or a handler for the BackKeyPress event, the app will exit.

You can work around this by calling Show on a different thread, as described in the following steps. Override BackKeyPress or create a handler for the BackKeyPress event. Set the Cancel to true to cancel the back key press action. Dispatch a method that shows the MessageBox. If the user chooses to leave the app, call Terminate(), otherwise, do nothing.

Upvotes: 4

Related Questions