我的应用从互联网上下载了几张图片,并在一个进度条中显示整体下载的进度。问题是当使用BackgroundTransferService时,进度没有正确更新。
这就是我所拥有的。
从我的PhoneApplicationPage_Loaded
我打电话下载我要下载的图片网址列表。
WebClient wc = new WebClient();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri(MyDownloadList), null);
然后在wc_DownloadStringCompleted
我在DownloadProgress
中注册了自定义事件DownloadManagerClass
,更新了用户界面pbDownloadProgress
然后我调用添加新BackgroundTransferRequest
的代码并注册TransferStatusChanged
和TransferProgressChanged
(位于DownloadManagerClass
中)。
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var up = e.Result;
Dispatcher.BeginInvoke(() => txtDownloadProgressText.Text = "Downloading");
downloadManager.DownloadProgress += (src, args) =>
{
Debug.WriteLine("Updating progress to " + args.Progress ` 100);
Dispatcher.BeginInvoke(() =>
{
if (pbDownloadProgress.IsIndeterminate)
{
pbDownloadProgress.IsIndeterminate = false;
pbDownloadProgress.Minimum = 0;
pbDownloadProgress.Maximum = 100;
}
pbDownloadProgress.Value = args.Progress * 100;
});
};
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += (s, ev) =>
{
downloadManager.DownloadImages(up);
};
bg.RunWorkerAsync();
}
}
我的DownloadImages
方法
internal void DownloadImages(String result)
{
List<String> URLs = ConvertStringToListUrls(result);
var localFile = IsolatedStorageFile.GetUserStoreForApplication();
if (!localFile.DirectoryExists(TRANSFERS_DIRECTORY))
{
localFile.CreateDirectory(TRANSFERS_DIRECTORY);
}
numberDownloadedItems = 0;
totalDownloadingItems = URLs.Count;
foreach (String item in URLs)
{
Debug.WriteLine("Adding Uri:" + uri);
Uri transferUri = new Uri(Uri.EscapeUriString(item), UriKind.RelativeOrAbsolute);
BackgroundTransferRequest transferRequest = new BackgroundTransferRequest(transferUri);
transferRequest.Method = "GET";
string downloadFile = transferFileName.Substring(transferFileName.LastIndexOf("/") + 1);
Uri downloadUri = new Uri(TRANSFERS_DIRECTORY + "/" + downloadFile, UriKind.RelativeOrAbsolute);
transferRequest.DownloadLocation = downloadUri;
transferRequest.TransferStatusChanged += new EventHandler<BackgroundTransferEventArgs>(transfer_TransferStatusChanged);
transferRequest.Tag = downloadFile;
transferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
BackgroundTransferService.Add(transferRequest);
Debug.WriteLine("Adding transferID: " + transferRequest.RequestId);
}
}
最后我的transfer_TransferStatusChanged
方法,主要在一个文件完成时调用DownloadProgress
事件。
void transfer_TransferProgressChanged(object sender, BackgroundTransferEventArgs e)
{
if (transfer.TransferStatus == TransferStatus.Completed && (transfer.StatusCode == 200 || transfer.StatusCode == 206)) {
if (RemoveTransferRequest(transfer.RequestId))
if (DownloadProgress != null) DownloadProgress(this, new ProgressEventArgs(++numberDownloadedItems, totalDownloadingItems));
}
}
每一步似乎都运行良好,但问题是在输出中我得到调试消息,指示应该更新的进度(如下所示),但进度条本身保持不变。
Removing transferID: {05ea9c10-025f-4e90-bc55-e9d36424b5f0}
10:37:27 AM:200: Updating progress to 14.2857142857143
Removing transferID: {e92f79fe-dc99-48b3-b0b1-76d3f5cedd51}
10:37:27 AM:651: Updating progress to 21.4285714285714
Removing transferID: {f61c8c9e-aa41-4e2a-8906-fca06961e69e}
10:37:28 AM:30: Updating progress to 28.5714285714286
Removing transferID: {46abc1df-37e6-432a-9b55-0adb3a8a2235}
我已经尝试删除调度程序使用,重新组织代码,但我似乎无法让它工作。
PS:我从代码中删除了一些异常以保持简单