如何在Xamarin.iOS / Mono中的任务中捕获异常?

时间:2013-03-21 19:41:32

标签: c# .net xamarin.ios

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中的错误?

1 个答案:

答案 0 :(得分:2)

您是否尝试在新任务委托中包含try-catch?像这样:

 Task.Factory.StartNew(() => {
        try {
             this.RefreshContent()
        } catch (Exception ex) {
             // Handle the exception
        }
  });
相关问题