C# BackgroundTransferService不更新UI
我的应用程序从互联网上下载多张图片,并在一个进度条中显示整体下载进度。问题在于,在使用BackgroundTransferService时,没有正确更新进度 这就是我所拥有的 从我的C# BackgroundTransferService不更新UI,c#,windows-phone-8,windows-phone,C#,Windows Phone 8,Windows Phone,我的应用程序从互联网上下载多张图片,并在一个进度条中显示整体下载进度。问题在于,在使用BackgroundTransferService时,没有正确更新进度 这就是我所拥有的 从我的PhoneApplicationPage\u加载的I调用download,下载我要下载的图像URL列表 WebClient wc = new WebClient(); wc.DownloadStringCompleted += wc_DownloadStringCompleted; wc.DownloadString
PhoneApplicationPage\u加载的I调用download,下载我要下载的图像URL列表
WebClient wc = new WebClient();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri(MyDownloadList), null);
然后在wc\u DownloadStringCompleted
中,我在我的DownloadManagerClass
中注册了一个自定义事件DownloadProgress
,该事件更新了UIpbDownloadProgress
然后调用添加新的BackgroundTransferRequest
的代码,并注册TransferStatusChanged
和TransferProgressChanged
(位于下载管理器类中)
和我的下载图像方法
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);
}
}
每一步似乎都很好,但问题是在输出上我得到了指示应该更新的进度的调试消息(如下所示),但progressbar本身保持不变
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}
我已经尝试删除dispatcher的用法,重新组织代码,但似乎无法使其正常工作
PS:我从代码中删除了一些异常捕获,以使其更简单。根据,Progress
的值介于0和100之间。如果Progress>=1,将其乘以100,则它将大于Progress barMaximum
。之前我没有将该值乘以100,但我试图改变这种想法,认为这就是问题所在。然而,在设置进度指示器的值之前,我将其最大值更改为100。根据,progress
的值介于0和100之间。如果Progress>=1,将其乘以100,则它将大于Progress barMaximum
。之前我没有将该值乘以100,但我试图改变这种想法,认为这就是问题所在。然而,在设置进度指示器的值之前,我将其最大值更改为100。
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}