我在尝试加载图像并使用 System.Threading.Task 显示它时遇到问题
我的代码如下
Task DownloadTask { get; set; }
public string Instrument { get; set; }
public PriceChartViewController(string Instrument) {
this.Instrument = Instrument;
DownloadTask = Task.Factory.StartNew(() => { });
}
private void LoadChart(ChartType chartType) {
NSData data = new NSData();
DownloadTask = DownloadTask.ContinueWith(prevTask => {
try {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
NSUrl nsUrl = new NSUrl(chartType.Uri(Instrument));
data = NSData.FromUrl(nsUrl);
}
finally {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
}
});
DownloadTask = DownloadTask.ContinueWith(t => {
UIImage image = new UIImage(data);
chartImageView = new UIImageView(image);
chartImageView.ContentScaleFactor = 2f;
View.AddSubview(chartImageView);
this.Title = chartType.Title;
}, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());
}
第二个 Continue with 似乎没有被调用?
最初,我的代码在没有后台处理的情况下如下所示,并且运行良好。
private void oldLoadChart(ChartType chartType) {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
NSUrl nsUrl = new NSUrl(chartType.Uri(Instrument));
NSData data = NSData.FromUrl(nsUrl);
UIImage image = new UIImage(data);
chartImageView = new UIImageView(image);
chartImageView.ContentScaleFactor = 2f;
View.AddSubview(chartImageView);
this.Title = chartType.Title;
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
}
有谁知道我做错了什么?