private void DownloadProgress(DownloadOperation download) { double percent = 100; if (download.Progress.TotalBytesToReceive > 0) { percent = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive; } DeviceContent content = Videos.FirstOrDefault(i => i.SourceName == download.ResultFile.Name); if (content == null) { content = EventVideos.FirstOrDefault(i => i.SourceName == download.ResultFile.Name); } if (content == null) { content = EventImages.FirstOrDefault(i => i.SourceName == download.ResultFile.Name); } if (content != null) { content.Progress = percent; } }
private async Task HandleDownloadAsync(DownloadOperation download, DeviceContent deviceContent) { try { deviceContent.Saving = true; Progress <DownloadOperation> progressCallback = new Progress <DownloadOperation>(DownloadProgress); await download.StartAsync().AsTask(CancellationToken.None, progressCallback); } catch (Exception ex) { _logger.Error($"An exception occured during download of [{deviceContent.SourceName}]", ex); } finally { deviceContent.Progress = 0; deviceContent.Saving = false; } }
internal async Task SaveDeviceContentAsync(DeviceContent e) { StorageFile destinationFile; try { StorageFolder folder = e as EventImage != null ? KnownFolders.PicturesLibrary : KnownFolders.VideosLibrary; destinationFile = await KnownFolders.VideosLibrary.CreateFileAsync( e.SourceName, CreationCollisionOption.ReplaceExisting); } catch (FileNotFoundException) { return; } DownloadOperation download = _downloader.CreateDownload(new Uri(string.Format("{0}/{1}", DeviceInstance.BaseAddress, e.SourceName)), destinationFile); await HandleDownloadAsync(download, e); }