使用NavigationHelper挂起时显示通用应用程序提示
本文关键字:应用程序 提示 显示 NavigationHelper 挂起 使用 | 更新日期: 2023-09-27 18:01:08
我在我的通用应用程序中使用NavigationHelper类,并且在应用程序级别上启用了BackPressed事件
#if WINDOWS_PHONE_APP
private async void HardwareButtons_BackPressed(object sender,
Windows.Phone.UI.Input.BackPressedEventArgs e)
{
...
}
#endif
在我的HardwareButtons_BackPressed事件中,我想通过调用以下代码来提示用户是否确定要退出应用程序:
UICommand ans = await this.GetLocatorViewModel.
DialogService.ShowMessagePrompt("Are you sure you want to quit?");
//NOTE: The validation below is not the complete code as I've removed
//the usage of my Enum for testing purpose but I would check the
//ans.Id
if (!object.ReferenceEquals(ans, null))
{
e.Handled = true;
}
我使用标准的异步方法来显示提示,它的定义如下:
public async Task<Object> ShowMessagePrompt(string content)
{
MessageDialog msgbox = new MessageDialog(content);
msgbox.Commands.Clear();
msgbox.Commands.Add(new UICommand { Label = "Yes", Id = 0 });
msgbox.Commands.Add(new UICommand { Label = "No", Id = 1 });
return await msgbox.ShowAsync();
}
如果我与上面的代码一起使用,因为它是从NavigationHelper类异步调用的,它会继续使用代码,并且仍然关闭应用程序。
如果我使用消息框。ShowAsync((。结果:
object ans=this。GetLocatorViewModel.DialogService.ShowMessagePrompt("是否确实要退出?"(.Result;
它显示提示ok,但一旦我点击Yes或No,它就会关闭提示,但它会导致我的应用程序挂起,以及在它没有执行后的任何代码,即检查提示的结果等。
知道我该怎么解决这个问题吗?
谢谢。
在显示消息对话框之前,将e.Handle设置为true。