MessageBox.Show in App Closing/Deactivated events

后端 未结 2 536
鱼传尺愫
鱼传尺愫 2021-01-29 04:16

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

相关标签:
2条回答
  • 2021-01-29 04:43

    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.

    0 讨论(0)
  • 2021-01-29 04:53

    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;
            }
    }
    
    0 讨论(0)
提交回复
热议问题