0

I have a MessageBox being shown in Application Closing/Deactivated methods in Windows Phone 7/8 application. It is used to warn the user for active timer being disabled because app is closing. The App Closing/Deactivated events are perfect for this, because putting logic in all application pages would be a killer - too many pages and paths for navigation. This works just fine - message box displays OK in WP7.

I also know for breaking changes in the API of WP8. There it is clearly stated that MessageBox.Show in Activated and Launching will cause exception.

The problem is that in WP8 the message box does not get shown on app closing. Code is executed without exception, but no message appears.

P.S. I've asked this on MS WP Dev forum but obviously no one knew.

Kamen
  • 3,575
  • 23
  • 35

2 Answers2

1

Move the msgBox code from the app closing events and into your main page codebehind. Override the on back key press event and place your code there. This is how it was done on 7.x:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Do you want to exit XXXXX?", "Application Closing", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
            {
                // Cancel default navigation
                e.Cancel = true;
            }
        }

FYI - On WP8 it looks like you have to dispatch the MsgBox Show to a new thread.

This prompts the user before the app ever actually starts to close in the event model. If the user accepts the back key press is allowed to happen, otherwise its canceled. You are not allowed to override the home button press, it must always go immediately to the home screen. You should look into background agents to persist your timer code through suspend / resume.

Inkog
  • 136
  • 3
  • It is not of use - OnBackKeyPress is still a page method - so I need to implement and maintain it in 10+ pages. What is worse, it does not resolve Fast switching - there no Back has been pressed :) – Kamen Nov 16 '12 at 22:03
  • You can still do this in App.xaml.cs: Just hook up to the navigating event of RootFrame and only trigger the above code if the backstack is empty (ie CanGoBack is false) – dotMorten Nov 16 '12 at 23:37
  • Of course, Inkog didn't bother to read the *breaking changes* link, so he doesn't realize his code will *also* crash the application. You need to *dispatch* the `MessageBox.Show` call *after* having set `e.Cancel = true;` ! – Claus Jørgensen Nov 20 '12 at 00:40
  • Claus, I don't think you bothered to read my post. Not only did I note the code was for 7.x, I also pointed out the dispatch change in 8 directly afterwards. I'm new around here, but I'd assume such a highly 'ranked' individual would fully read a post before commenting on how someone else failed to. – Inkog Nov 22 '12 at 00:07
0

Register BackKeyPress event on RootFrame.

RootFrame.BackKeyPress += BackKeyPressed;
private void BackKeyPressed(object sender, CancelEventArgs e)
    {
        var result = (MessageBox.Show("Do you want to exit XXXXX?", "Application Closing", MessageBoxButton.OKCancel));
        if (result == MessageBoxResult.Cancel)
        {
            // Cancel default navigation
            e.Cancel = true;
        }
}
CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
lieska
  • 1