【发布时间】:2016-01-12 22:12:21
【问题描述】:
在 WinRT 中。我有一个后台下载方法,下载进度应该在 UI 部分更新。
我的代码是
public async static Task DownloadSingleFile(string name, SoundClass sc)
{
var dl = new BackgroundDownloader();
dl.CostPolicy = BackgroundTransferCostPolicy.Always;
file = await localSoundsFolder.CreateFileAsync(name, CreationCollisionOption.ReplaceExisting);
var d = dl.CreateDownload(new Uri(uriToDownloadFrom), file);
d.Priority = BackgroundTransferPriority.Default;
var progressCallback = new Progress<DownloadOperation>(DownloadProgress);
try
{
await d.StartAsync().AsTask(cancellationToken.Token, progressCallback);
CancellationTokenSource token = Utility.cancellationList[sc];
if (token != null)
{
token.Cancel();
Utility.cancellationList.Remove(sc);
Debug.WriteLine("The sc has been removed from the download list");
}
}
catch
{
return;
}
}
而下载方式是这样的
private static void DownloadProgress(DownloadOperation download)
{
Debug.WriteLine("Callback");
var value = download.Progress.BytesReceived * 100 / download.Progress.TotalBytesToReceive;
Debug.WriteLine("The bytesReceived is {0} and total bytes is {1}", download.Progress.BytesReceived.ToString(), download.Progress.TotalBytesToReceive.ToString());
new System.Threading.ManualResetEvent(false).WaitOne(10);
//Update the UI here
if (download.Progress.Status == BackgroundTransferStatus.Completed || value >= 100)
{
//Perform opertaion
}
}
我面临的问题是,由于发生了多个下载操作,我无法直接执行更新 UI 的操作。我想知道如何发送一个绑定到 UI 并有助于更新操作的参数 DownloadProgress 方法。
【问题讨论】:
-
您能否展示或分享一些资源,这些资源有助于实现具有实时进度和取消选项的多文件下载
标签: c# windows-runtime winrt-xaml uwp