Xamarin.iOS 6.2.1、Xamarin Studio 4
我在 ViewDidAppear()
中有这段代码:
this.refreshTask = Task.Factory.StartNew(() => this.RefreshContent());
try
{
this.refreshTask.Wait();
}
catch(AggregateException aggEx)
{
aggEx.Handle(x => false);
}
RefreshContent()
方法在访问数组时导致 IndexOutOfBoundsException
。如果我直接运行该方法,我可以看到这一点。
如果我在任务中运行它,如上所述,应用程序不会失败,我最终会得到一个空的 TableView 。根据这篇文章:http://msdn.microsoft.com/en-us/library/dd537614.aspx上面的代码应该处理异常。
但是,永远不会触发 AggregateException
。
我在这里做错了什么?还是 Xamarin.iOS/Mono 中的错误?
最佳答案
您是否尝试过在新任务委托(delegate)中包含一个 try-catch?像这样:
Task.Factory.StartNew(() => {
try {
this.RefreshContent()
} catch (Exception ex) {
// Handle the exception
}
});
关于c# - 如何在 Xamarin.iOS/Mono 的任务中捕获异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15556586/