在的MessageBox.show应用关闭/停用事件(MessageBox.Show in App

2019-08-02 19:55发布

我有一个消息框被显示在申请截止/ Windows Phone中7/8的应用已停用的方法。 它被用来警告为活动定时器被用户禁用,因为应用程序正在关闭。 该应用程序关闭/停用的事件是适合这个,因为把逻辑中的所有应用程序页面将是一个杀手锏 - 太多的页面和路径导航。 这只是正常 - 消息框,在WP7显示OK。

我也知道WP8的API中的重大更改 。 在那里,它是明确表示,在MessageBox.Show激活和启动会导致异常。

问题是,在WP8的消息框不会被显示在应用程序关闭。 代码无一例外执行,但不会出现任何消​​息。

PS我问过这个关于MS WP开发论坛,但显然没有人知道。

Answer 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.



Answer 2:

注册于RootFrame BackKeyPress事件。

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


文章来源: MessageBox.Show in App Closing/Deactivated events