我有一个课程,不应该向最终用户显示任何对话框。如果该用户传递了错误的文件路径,我尝试抛出异常并在适当的类中处理它。但是,尽管有“throw”指令,Visual Studio 仍会显示异常对话框并在它发生后中断应用程序(调试模式)。在发布模式下,应用程序在给出错误的文件路径后崩溃。我究竟做错了什么?
GuyManager.cs:
private IStorageFile latestGuyFile;
public IStorageFile LatestGuyFile { get { return latestGuyFile; } }
public string Path { get; set; }
public async void ReadGuyAsync()
{
if (String.IsNullOrWhiteSpace(Path))
return;
try
{
latestGuyFile = await StorageFile.GetFileFromPathAsync(Path);
}
catch (Exception ex)
{
Debug.WriteLine("Error occured: " +ex.Message);
Debug.WriteLine(ex.StackTrace);
throw;
}
MainPage.xml.cs:
private async void loadGuy_Click(object sender, RoutedEventArgs e)
{
try
{
guyManager.ReadGuyAsync();
}
catch (Exception ex)
{
MessageDialog dialog = new MessageDialog("Error" + ex.Message);
await dialog.ShowAsync();
}
}
我发现您的 Click 事件处理程序有问题。您将其定义为异步,但您没有在等待任何东西。
您应该更改 ReadGuyAsync 方法以返回 Task 而不是 void,如下所示:
public async Task ReadGuyAsync()
在 loadGuy_Click 方法中,你应该等待它:
await guyManager.ReadGuyAync();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句