【发布时间】:2015-07-05 05:00:54
【问题描述】:
我在我的通用应用程序中使用 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 类异步调用的,它会继续使用代码并仍然关闭应用程序。
如果我使用 msgbox.ShowAsync().Result:
object ans = this.GetLocatorViewModel.DialogService.ShowMessagePrompt("您确定要退出吗?").Result;
它显示提示正常,但是一旦我点击是或否,它会关闭提示,但它会导致我的应用程序挂起,并且在它没有被执行之后的任何代码,即检查提示的结果等...
知道如何解决这个问题吗?
谢谢。
【问题讨论】:
标签: c# windows-phone-8.1 win-universal-app winrt-async