C# 用于在Windows Phone中退出的自定义MessageBox
如果用户点击游戏应用程序主页上的“后退”,我会显示一个消息框 通常的解决办法C# 用于在Windows Phone中退出的自定义MessageBox,c#,windows-phone-8,C#,Windows Phone 8,如果用户点击游戏应用程序主页上的“后退”,我会显示一个消息框 通常的解决办法 MessageBoxResult res = MessageBox.Show(txt, cap, MessageBoxButton.OKCancel); if (res == MessageBoxResult.OK) { e.Cancel = false; return; } 不适用于我,因为我需要这些按钮进行本地化,而不是使用手机的本地化,而是使用应用程序的选
MessageBoxResult res = MessageBox.Show(txt, cap, MessageBoxButton.OKCancel);
if (res == MessageBoxResult.OK)
{
e.Cancel = false;
return;
}
不适用于我,因为我需要这些按钮进行本地化,而不是使用手机的本地化,而是使用应用程序的选定语言(即,如果用户的手机具有英语语言环境,并且他已将应用程序的语言设置为法语,则按钮应为“Oui”和“Non”,而不是默认的“OK”和“Cancel”)
我尝试了以下方法,它在视觉上有效:
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
}
backkeypress上受保护的覆盖无效(CancelEventArgs e)
{
//一些条件
e、 取消=真;
字符串quitchext=DeviceWrapper.Localize(“QUIT_TEXT”);
字符串quitCaption=DeviceWrapper.Localize(“QUIT_CAPTION”);
字符串quitOk=DeviceWrapper.Localize(“disease”);
string quitCancel=DeviceWrapper.Localize(“MESSAGEBOX_CANCEL”);
Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
标题:,
quitText,
新列表{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=退出游戏
{
e、 取消=假;
返回;
}
},
无效);
//更多功能
}
但它不会退出应用程序
我应该使用哪种方法?我不会使用“Terminate”,因为它是一个相当大的应用程序,这样退出不好。它不会退出,因为
BeginShowMessageBox()
是异步的。这意味着调用将立即返回,因为您将e.Cancel
设置为true
,所以应用程序将永远不会关闭(当您的事件处理程序将被执行时,调用方法结束而不退出)
只需等待用户关闭对话框,将e.Cancel
设置为正确的值(忽略AsyncCallback
参数)。首先删除回调:
IAsyncResult asyncResult = Guide.BeginShowMessageBox(
quitCaption, quitText, new List<string> { quitOk, quitCancel },
0, MessageBoxIcon.Error, null, null);
int? result = Guide.EndShowMessageBox(asyncResult);
if (result.HasValue && result.Value == 0)
e.Cancel = false;
最后,您可以检查其返回值(就像您在原始回调中所做的那样):
int? result = Guide.EndShowMessageBox(asyncResult);
if (result.HasValue && result.Value == 0)
e.Cancel = false;