在Windows Phone中自定义退出消息框
本文关键字:退出 消息 自定义 Windows Phone | 更新日期: 2023-09-27 18:14:09
如果用户在游戏应用程序的主页点击"返回",我将显示一个消息框。
通常的解决方案
MessageBoxResult res = MessageBox.Show(txt, cap, MessageBoxButton.OKCancel);
if (res == MessageBoxResult.OK)
{
e.Cancel = false;
return;
}
不适合我,因为我需要这些按钮本地化不与手机的本地化,但使用应用程序的选择语言(即,如果用户的手机有英语语言环境,他已经设置了一个应用程序的语言为法语,按钮应该是"Oui"answers"Non",而不是默认的"OK"answers"取消")。
我尝试了以下方法,它在视觉上起作用:
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
}
但是它不会退出应用程序。
我应该使用哪种方法?我不打算用"终止"因为它是一个相当大的应用程序,这样退出是不好的
它不退出,因为BeginShowMessageBox()
是异步的。这意味着调用将立即返回,因为你将e.Cancel
设置为true
,那么应用程序将永远不会关闭(当你的事件处理程序将被执行调用方法结束而不退出)。
等待用户关闭对话框将e.Cancel
设置为合适的值(省略AsyncCallback
参数)。首先删除回调:
IAsyncResult asyncResult = Guide.BeginShowMessageBox(
quitCaption, quitText, new List<string> { quitOk, quitCancel },
0, MessageBoxIcon.Error, null, null);
然后等待对话框关闭:
asyncResult.AsyncWaitHandle.WaitOne();
最后你可以检查它的返回值(就像你在最初的回调中所做的那样):
int? result = Guide.EndShowMessageBox(asyncResult);
if (result.HasValue && result.Value == 0)
e.Cancel = false;