Windows Phone中BackgroundTransferRequest的TransferStatusChange
本文关键字:TransferStatusChange BackgroundTransferRequest Phone Windows | 更新日期: 2023-09-27 18:06:29
我正在实现一个应用程序,用户可以选择下载资产(大约750MB的图像,每个大小约为50KB)。我不能使用常规的HttpClient做到这一点,因为如果用户按下home键,SendAsync/GetAsync api在后台不起作用,当用户回到应用程序时崩溃。
我唯一的选择是使用BackgroundTransferService,我使用如下:-
for(...)//calling the download function here
DownloadImageToIsoStore(planImageUri, cemetery_id + "_plan_1.gif", countryName);
public void DownloadImageToIsoStore(Uri imageUri, string imageName,string countryName)
{
while ((BackgroundTransferService.Requests.Count()) >= 25) { Thread.Sleep(100); }//do not add if count is 25 since that is the limit. added this to wait till space frees up in the queue
BackgroundTransferRequest backgroundTransferRequest = new BackgroundTransferRequest(imageUri);
backgroundTransferRequest.Method = "GET";
backgroundTransferRequest.DownloadLocation = new Uri("shared/transfers/" + imageName, UriKind.RelativeOrAbsolute);//shared/transfers is the required directory
backgroundTransferRequest.Tag = countryName;//will check this when download is completed
backgroundTransferRequest.TransferPreferences = TransferPreferences.AllowCellularAndBattery;
BackgroundTransferService.Add(backgroundTransferRequest);
backgroundTransferRequest.TransferStatusChanged += new EventHandler<BackgroundTransferEventArgs>(backgroundTransferRequest_TransferStatusChanged);
backgroundTransferRequest.TransferProgressChanged += new EventHandler<BackgroundTransferEventArgs>(backgroundTransferRequest_TransferProgressChanged);
}
void backgroundTransferRequest_TransferProgressChanged(object sender, BackgroundTransferEventArgs e)
{
long bytes = e.Request.BytesReceived;
Debug.WriteLine(bytes);
}
void backgroundTransferRequest_TransferStatusChanged(object sender, BackgroundTransferEventArgs e)
{
BackgroundTransferRequest backgroundTransferRequest = e.Request;
BackgroundTransferService.Remove(backgroundTransferRequest);
}
下载成功完成,但由于某种原因,backgroundTransferRequest_TransferProgressChanged和backgroundTransferRequest_TransferStatusChanged事件没有被调用,我无法为任何额外的下载释放下载队列。
请帮忙!
谢谢
问题是我正在UI线程上运行调用downloadadimagetoisostore的循环。
传输状态改变和进度改变事件显然也需要UI线程。只要我将调用函数移动到一个单独的线程中,状态和进度更改事件就会像预期的那样开始触发。
我相信这不是原因。您将请求添加到后台服务,而没有附加处理程序。在将请求添加到后台服务之前,必须附加处理程序。