private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
var msg = new MessageDialog("Confirm Close");
var okBtn = new UICommand("OK");
var cancelBtn = new UICommand("Cancel");
msg.Commands.Add(okBtn);
msg.Commands.Add(cancelBtn);
IUICommand result = await msg.ShowAsync();
}
主要问题是当按下后退按钮时,消息显示1-2秒,应用程序关闭。 怎么办?
答案 0 :(得分:1)
异步工作正常我认为,您没有将 e.Handeled 设置为 true ,这意味着会处理更多BackPressed事件并退出应用。试试这样:
private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
var msg = new MessageDialog("Confirm Close");
var okBtn = new UICommand("OK");
var cancelBtn = new UICommand("Cancel");
msg.Commands.Add(okBtn);
msg.Commands.Add(cancelBtn);
IUICommand result = await msg.ShowAsync();
}
还要记住,您的应用必须能够通过BackButton退出,因此上述代码必须以某种方式进行修改。
答案 1 :(得分:0)
试试这段代码:
var messgeDialog = new MessageDialog("Are you sure you want to close", "Are you sure?");
messgeDialog.Commands.Add(new UICommand("Yes"));
messgeDialog.Commands.Add(new UICommand("No"));
messgeDialog.DefaultCommandIndex = 0;
messgeDialog.CancelCommandIndex = 1;
var result = await messgeDialog.ShowAsync();
if (result.Label.Equals("Yes"))
{
//Do something when a user pressed yes
}
else
{
//User pressed no
}