首先是代码,它运行没有错误,但没有涵盖我需要它的所有内容:
public async Task SaveData()
{
// MessageDialog restartMessage;
// ^^ This was made global and is defined in SafeToSave()
// Check if it's safe to save
await SafeToSave();
if (restartMessage != null)
{
CoreDispatcher cD = Window.Current.CoreWindow.Dispatcher;
await cD.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
await restartMessage.ShowAsync();
// This WILL invoke 'Application.Current.Exit()'
});
return;
// Issue is right around here...
}
// Other code...
}
基本前提是,如果保存不安全,消息对话框将告诉用户应用程序需要重新启动,然后在他们点击“确定”按钮时执行“Application.Current.Exit()”。
我的问题是我需要我的代码在重新启动时停止执行。现在'return'只结束这个方法,但之后它会继续在调用SaveData()的方法中做其他事情,直到我的restartMessage生效,这不好。
我考虑将“Application.Current.Exit()”放在“return”语句之上,但这会在我的错误消息出现之前关闭。这也不好。
另一个解决方案是跳过整个 CoreDispatcher 并且只运行 ShowAsync() 本身,除非这会触发不同的已知错误。基本上,我有一些 MessageDialogs 调用 SaveData() 方法并在另一个 MessageDialog 已经打开时打开一个 MessageDialog,这同样不好。
就是这样:我需要一些东西来阻止我的代码表单执行,而不会杀死整个应用程序或阻止我的错误消息显示。我能做些什么?