void BeginDownloading () { // Show the user that data is about to be downloaded UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true; // Retrieve the rss feed from the server var downloader = new GzipWebClient (); downloader.DownloadStringCompleted += DownloadCompleted; downloader.DownloadStringAsync (RssFeedUrl); }
async Task<byte[]> GetImageData(App app) { byte[] data = null; try { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true; using (var c = new GzipWebClient ()) data = await c.DownloadDataTaskAsync (app.ImageUrl); } finally { UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false; } return data; }
void BeginDownloadingImage (App 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.ImageUrl); } 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.Image = 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 = Controller.TableView.VisibleCells.Where (c => c.Tag == Controller.Apps.IndexOf (app)).FirstOrDefault (); if (cell != null) cell.ImageView.Image = app.Image; }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext ()); }