0
在WinRT中。我在后台方法下载并下载进度应在UI部分更新。如何在使用BackgroundDownloader并行下载时更新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
}
}
我现在面临的问题是,因为我有多个下载操作发生的事情,我不能直接执行该操作更新用户界面。我想知道如何发送参数DownloadProgress
方法,该方法绑定到UI并帮助更新操作。
能否请你告诉或共享一些资源,这是多个文件的执行有帮助与实时进度和取消选项下载 –