如何为metro windows app多次下载获得相同的进度?

时间:2015-02-20 17:35:03

标签: c# windows-8 windows-store-apps windows-8.1

我尝试为windows商店制作metro应用程序。在应用程序的开始我尝试下载许多文件。在下载过程中,我想为所有下载文件提供一个进度条。因此,我尝试在此示例中调整Background-Transfer-Sample原因所有文件都有自己的进度,而不仅仅是一个。 所以暂时我有进度条,它只是逐个文件进展。 如何为metro windows app多次下载获得相同的进度? 我的代码的一部分:

StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFolder dataFolder = await localFolder.CreateFolderAsync("Contenu", CreationCollisionOption.OpenIfExists);
var backgroundDownloader = new BackgroundDownloader();
foreach (var img in contentToDownload)
            {
                loadingBar.Value = 0;
                var uri = new Uri(img);
                StorageFile newFile = await dataFolder.CreateFileAsync(Path.GetFileName(img), CreationCollisionOption.ReplaceExisting);
                DownloadOperation downloadOperation = backgroundDownloader.CreateDownload(uri, newFile);
                await HandleDownloadAsync(downloadOperation);
            }


private async Task HandleDownloadAsync(DownloadOperation downloadOperation)
        {
            var progress = new Progress<DownloadOperation>(ProgressCallback);
            await downloadOperation.StartAsync().AsTask(progress);
        }

private void ProgressCallback(DownloadOperation obj)
        {
            double progress
            = ((double)obj.Progress.BytesReceived / obj.Progress.TotalBytesToReceive);
            loadingBar.Value = progress * 100;
            Debug.WriteLine((int)loadingBar.Value);
        }

1 个答案:

答案 0 :(得分:0)

有一段时间我找到了解决方法。 这是解决方法。

首先在代码顶部声明这个

--cherry-pick

在你的函数ProgressCallback中添加这行代码

private List<DownloadOperation> allDownloads = new List<DownloadOperation>();
private List<ResponseInformation> responseList = new List<ResponseInformation();
private ulong TOTALBYTES = 0;
private ulong BYTES = 0;

然后在函数HandleDownloadAsync中添加此代码

private void ProgressCallback(DownloadOperation obj)
{
   TOTALBYTES += obj.Progress.TotalBytesToReceive;
   BYTES += obj.Progress.BytesReceived;
}

希望这会有所帮助。