嘿大家我从msdn的后台下载转移中获取此代码,我稍微修改了一下
private async Task HandleDownloadAsync(DownloadOperation download, bool start)
{
bool finish = false;
try
{
LogStatus("Running: " + download.Guid, NotifyType.StatusMessage);
// Store the download so we can pause/resume.
activeDownloads.Add(download);
DownloadData newData = new DownloadData();
newData.DownloadOp = download;
downloadData.Add(newData);
Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
if (start)
{
// Start the download and attach a progress handler.
await download.StartAsync().AsTask(cts.Token, progressCallback);
}
else
{
// The download was already running when the application started, re-attach the progress handler.
await download.AttachAsync().AsTask(cts.Token, progressCallback);
}
ResponseInformation response = download.GetResponseInformation();
LogStatus(String.Format(CultureInfo.CurrentCulture, "Completed: {0}, Status Code: {1}",
download.Guid, response.StatusCode), NotifyType.StatusMessage);
}
catch (TaskCanceledException)
{
LogStatus("Canceled: " + download.Guid, NotifyType.StatusMessage);
}
catch (Exception ex)
{
if (!IsExceptionHandled("Execution error", ex, download))
{
throw;
}
}
finally
{
finish = true;
}
if (finish == true)
{
//Something todo here
activeDownloads.Remove(download);
downloadData.RemoveAt(ind);
finish = false;
}
}
我在模拟器上尝试了这个并且在下载进度完成后立即执行了“finally”行,但是我在真实设备上尝试了这个并且没有执行finally行。它不会给出任何错误,并且在catch行上没有任何结果。