使用并行下载的 BackgroundDownloader 时如何更新 UI?
How to update the UI when using a BackgroundDownloader with parallel downloads?
在 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
方法。
你可以只使用 lambdas:
int downloadId = ...;
var progressCallback = new Progress<DownloadOperation>(x => DownloadProgress(x, downloadId));
那么你的进度更新器就可以使用它了:
private static void DownloadProgress(DownloadOperation download, int downloadId)
{
... // use downloadId
}
在 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
方法。
你可以只使用 lambdas:
int downloadId = ...;
var progressCallback = new Progress<DownloadOperation>(x => DownloadProgress(x, downloadId));
那么你的进度更新器就可以使用它了:
private static void DownloadProgress(DownloadOperation download, int downloadId)
{
... // use downloadId
}