MessageBox.Show in App Closing / Deactivated Events

I have a MessageBox that appears in the Closing / Deactivated Methods application in a Windows Phone 7/8 application. It is used to alert the user that the active timer is disabled because the application is closing. App Closing / Deactivated events are perfect for this because the logic on all application pages will be a killer - there are too many pages and paths to navigate. This works great - the message box is displayed in WP7.

I also know the violation of changes in the WP8 API . It clearly states that MessageBox.Show will throw an exception when activated and launched.

The problem is that in WP8 the message box does not appear when you close the application. The code is executed without exception, but a message does not appear.

PS I asked about this on the MS WP Dev forum, but obviously no one knew.

0
source share
2 answers

Move the msgBox code from application close events and to your main page. Override the back key enable event and put your code there. This 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 need to send the MsgBox Show to a new thread.

This raises a request before the application really begins to close in the event model. If the user accepts the return key, then pressing the button is allowed, otherwise cancel it. You cannot redefine pressing the home button; it should always go to the main screen immediately. You should look at background agents to save your timer code using the suspend / resume function.

+1
source

Register your 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; } } 
0
source

Source: https://habr.com/ru/post/1483708/


All Articles