Windows Phone 8:如果按返回键,如何在退出应用程序之前立即显示消息框? [英] Windows Phone 8: How to show messagebox right before exiting the App if pressed Back key?

查看:109
本文介绍了Windows Phone 8:如果按返回键,如何在退出应用程序之前立即显示消息框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,当用户至少退出一次时需要引起用户的注意.所以我得到下面的代码来显示一个消息框.我不知道,如果用户已阅读该消息,如何才能真正退出该应用程序?因为似乎后退键事件总是会出现在我设置的呼叫中(OnBackKeyPress),或者有什么好方法可以处理显示消息框而又不会弄乱覆盖的BackKey?因为如果屏幕上又弹出一个窗口并且用户按下了回车键,那么如果我尝试自己处理回车键,似乎会出现一些异常.

I have an app that needs to get user's attention when user exit the app at least once. So I get the code below to show a messagebox. What I don't know is how do I really exit the app if user has read the message? Because it seems the back key event will always come to the call I setup (OnBackKeyPress) Or what is a good way to handle showing a messagebox without messing around overriding BackKey? Because if have another pop up on screen and user pressed back key, it seems I got some exception if I tried to handle backkey myself.

我的理想情况是,一旦按下消息框的退出"按钮,该应用程序将立即关闭.如果按取消,它将返回而不退出.请帮忙.谢谢!

My ideal situation is the app will close immediately once s/he pressed my Exit button of the messagebox. If pressed cancel, it will go back without exiting. Please help. Thanks!

我用过的东西...但是效果不佳

Something I used... but not working well

private void OnBackKeyPressed(object sender, CancelEventArgs e)
    {
        e.Cancel = true;

        CheckBox checkBox = new CheckBox()
        {
            Content = "Do not ask me again",
            Margin = new Thickness(0, 14, 0, -2)
        };

        TiltEffect.SetIsTiltEnabled(checkBox, true);

        CustomMessageBox messageBox = new CustomMessageBox()
        {
            Caption = "Would you like to stop and exit?",
            Message =
                "If you want to continue listen music while doing other stuff, please use Home key instead of Back key",
            Content = checkBox,
            LeftButtonContent = "Exit",
            RightButtonContent = "Cancel",
        };

        messageBox.Dismissed += (s1, e1) =>
        {
            switch (e1.Result)
            {
                case CustomMessageBoxResult.LeftButton: //Exit
                    return;// What to do here??
                case CustomMessageBoxResult.RightButton: //Cancel
                case CustomMessageBoxResult.None:
                    break;
                default:
                    break;
            }
        };
        messageBox.Show();
    }

}

推荐答案

如果您真的需要按钮说退出",则必须使用ColinE或Paul Annetts解决方案.我个人将尝试以另一种方式来编写消息,并使用如下所示的普通消息框:

If you really need the button to say "Exit" you would have to use either ColinE or Paul Annetts solution. Personally I would try formulating the message in another way and use the normal message box like this:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
   string caption = "Stop music and exit?";
   string message ="If you want to continue listen music while doing other stuff, please use Home key instead of Back key. Do you still want to exit?";
   e.Cancel = MessageBoxResult.Cancel == MessageBox.Show(message, caption, MessageBoxButton.OKCancel);

   base.OnBackKeyPress(e);
}

这将显示一个消息框,但带有确定"和取消"按钮,并且如果用户按下确定"将直接退出.

This will show a message box but with "Ok" and "Cancel" button instead and will exit directly if user presses "Ok".

对于不再询问",也许可以在设置页面上添加设置.如果用户想再次查看该对话框,则在用户首次在确定"和取消"之间进行选择时添加第二个对话框.

And for the "Do not ask again", maybe add a settings on a setting page instead. And add a second dialog the first time the user choose between "Ok" and "Cancel" if he or she wants to see the dialog again.

这篇关于Windows Phone 8:如果按返回键,如何在退出应用程序之前立即显示消息框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆