用于退出 Windows Phone 的自定义 MessageBox [英] Custom MessageBox for exit in Windows Phone

查看:21
本文介绍了用于退出 Windows Phone 的自定义 MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户在游戏应用程序的主页上点击返回",我会显示一个消息框.

I am showing a message box if a user taps "back" at the main page of a game application.

通常的解决方案

MessageBoxResult res = MessageBox.Show(txt, cap, MessageBoxButton.OKCancel);
    if (res == MessageBoxResult.OK)
    {
        e.Cancel = false;
        return;
    }

对我不起作用,因为我需要这些按钮不是通过手机的本地化而是使用应用程序的选定语言进行本地化(即,如果用户的手机具有英语区域设置并且他已将应用程序的语言设置为法语,则按钮应该是Oui"和Non",而不是默认的OK"和Cancel").

doesn't work for me, because I need those buttons to be localized not with a phone's localization but using app's selected language (i.e. if user's phone has english locale and he has set an app's language to be french, the buttons should be "Oui" and "Non" instead of default "OK" and "Cancel").

我尝试了以下方法并且它在视觉上有效:

I have tried the following approach and it works visually:

protected override void OnBackKeyPress(CancelEventArgs e)
{
    //some conditions
    e.Cancel = true;
    string quitText = DeviceWrapper.Localize("QUIT_TEXT");
    string quitCaption = DeviceWrapper.Localize("QUIT_CAPTION");
    string quitOk = DeviceWrapper.Localize("DISMISS");
    string quitCancel = DeviceWrapper.Localize("MESSAGEBOX_CANCEL");
    Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
        quitCaption, 
        quitText,
        new List<string> { quitOk, quitCancel }, 
        0, 
        Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.Error,
        asyncResult =>
        {
            int? returned = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(asyncResult);
            if (returned.Value == 0) //first option = OK = quit the game
            {
                e.Cancel = false;
                return;
            }
        }, 
        null);
    //some more features
}

但它不会退出应用程序.

but it doesn't quit an application.

我应该使用哪种方法?我不打算使用终止",因为它是一个相当大的应用程序,这样退出并不好.

Which approach should I use? I am not to use "Terminate" because it's a fairly big app and it's not good to quit it that way.

推荐答案

它不会退出,因为 BeginShowMessageBox() 是异步的.这意味着调用将立即返回,并且因为您将 e.Cancel 设置为 true 然后应用程序将永远不会关闭(当您的事件处理程序将被执行时,调用方法结束而不退出).

It doesn't quit because BeginShowMessageBox() is asynchronous. It means that call will return immediatly and because you set e.Cancel to true then application won't ever close (when your event handler will be executed calling method ended without quitting).

只需等待用户关闭对话框以将 e.Cancel 设置为正确的值(省略 AsyncCallback 参数).首先移除回调:

Just wait user closes the dialog to set e.Cancel to proper value (omitting AsyncCallback parameter). First remove callback:

IAsyncResult asyncResult = Guide.BeginShowMessageBox(
    quitCaption, quitText, new List<string> { quitOk, quitCancel }, 
    0, MessageBoxIcon.Error, null, null);

然后等待对话框关闭:

asyncResult.AsyncWaitHandle.WaitOne();

最后您可以检查它的返回值(就像您在原始回调中所做的那样):

Finally you can check its return value (as you did in your original callback):

int? result = Guide.EndShowMessageBox(asyncResult);
if (result.HasValue && result.Value == 0)
    e.Cancel = false;

这篇关于用于退出 Windows Phone 的自定义 MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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