void BeginDownloadingImage(string ImageUrl, NSIndexPath indexPath, UITableView tableView) { byte[] data = null; UIImage returnImage = null; DownloadTask = DownloadTask.ContinueWith(prevTask => { try { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true; using (var c = new GzipWebClient()) data = c.DownloadData(ImageUrl); } finally { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false; } }); DownloadTask = DownloadTask.ContinueWith(t => { returnImage = UIImage.LoadFromData(NSData.FromArray(data)); var cell = tableView.CellAt(indexPath); if (cell != null) { cell.ImageView.Image = returnImage; } //The Download task }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext()); }
void BeginDownloadingImage(RSSFeedItem app, NSIndexPath path) { // Queue the image to be downloaded. This task will execute // as soon as the existing ones have finished. byte[] data = null; DownloadTask = DownloadTask.ContinueWith(prevTask => { try { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true; using (var c = new GzipWebClient()) data = c.DownloadData(app.Image); } finally { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false; } }); // When the download task is finished, queue another task to update the UI. // Note that this task will run only if the download is successful and it // uses the CurrentSyncronisationContext, which on MonoTouch causes the task // to be run on the main UI thread. This allows us to safely access the UI. DownloadTask = DownloadTask.ContinueWith(t => { // Load the image from the byte array. app.TheImage = UIImage.LoadFromData(NSData.FromArray(data)); // Retrieve the cell which corresponds to the current App. If the cell is null, it means the user // has already scrolled that app off-screen. var cell = TableView.VisibleCells.Where(c => c.Tag == viewModel.FeedItems.IndexOf(app)).FirstOrDefault(); if (cell != null) { cell.ImageView.Image = app.TheImage; } }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext()); }
void BeginDownloadingImage (RSSFeedItem app, NSIndexPath path) { // Queue the image to be downloaded. This task will execute // as soon as the existing ones have finished. byte[] data = null; DownloadTask = DownloadTask.ContinueWith (prevTask => { try { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true; using (var c = new GzipWebClient ()) data = c.DownloadData (app.Image); } finally { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false; } }); // When the download task is finished, queue another task to update the UI. // Note that this task will run only if the download is successful and it // uses the CurrentSyncronisationContext, which on MonoTouch causes the task // to be run on the main UI thread. This allows us to safely access the UI. DownloadTask = DownloadTask.ContinueWith (t => { // Load the image from the byte array. app.TheImage = UIImage.LoadFromData (NSData.FromArray (data)); // Retrieve the cell which corresponds to the current App. If the cell is null, it means the user // has already scrolled that app off-screen. var cell = TableView.VisibleCells.Where (c => c.Tag == viewModel.FeedItems.IndexOf (app)).FirstOrDefault (); if (cell != null) cell.ImageView.Image = app.TheImage; }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext ()); }