/// <summary> /// 监视指定的后台下载任务 /// </summary> /// <param name="download">后台下载任务</param> private async Task HandleDownloadAsync(DownloadManage.HandleModel model) { try { DownloadProgress(model.downOp); //进度监控 Progress <DownloadOperation> progressCallback = new Progress <DownloadOperation>(DownloadProgress); await model.downOp.AttachAsync().AsTask(model.cts.Token, progressCallback); //保存任务信息 // StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFolder DowFolder = await KnownFolders.VideosLibrary.CreateFolderAsync("Bili-Down", CreationCollisionOption.OpenIfExists); StorageFile file = await DowFolder.GetFileAsync(model.Guid + ".bili"); //用Url编码是因为不支持读取中文名 //含会出现:在多字节的目标代码页中,没有此 Unicode 字符可以映射到的字符。错误 string path = WebUtility.UrlDecode(await FileIO.ReadTextAsync(file)); StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path); StorageFile files = await folder.CreateFileAsync(model.Guid + ".json", CreationCollisionOption.OpenIfExists); //await StorageFile.GetFileFromPathAsync(path+@"\" + model.Guid + ".json"); string json = await FileIO.ReadTextAsync(files); DownloadManage.DownModel info = JsonConvert.DeserializeObject <DownloadManage.DownModel>(json); info.downloaded = true; string jsonInfo = JsonConvert.SerializeObject(info); StorageFile fileWrite = await folder.CreateFileAsync(info.Guid + ".json", CreationCollisionOption.ReplaceExisting); await FileIO.WriteTextAsync(fileWrite, jsonInfo); //移除正在监控 HandleList.Remove(model.downModel.Guid); GetDownOk_New(); } catch (TaskCanceledException) { //取消通知 //ToastTemplateType toastTemplate = ToastTemplateType.ToastText01; //XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); //XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text"); //IXmlNode toastNode = toastXml.SelectSingleNode("/toast"); //((XmlElement)toastNode).SetAttribute("duration", "short"); //toastTextElements[0].AppendChild(toastXml.CreateTextNode(String.Format("取消任务{0}", model.downModel.title))); //ToastNotification toast = new ToastNotification(toastXml); //ToastNotificationManager.CreateToastNotifier().Show(toast); } catch (Exception ex) { WebErrorStatus error = BackgroundTransferError.GetStatus(ex.HResult); return; } finally { list_Downing.Items.Remove(model); } }
//进度发生变化时,通知更新UI private void DownloadProgress(DownloadOperation op) { try { DownloadManage.HandleModel test = null; foreach (DownloadManage.HandleModel item in list_Downing.Items) { if (item.downOp.Guid == op.Guid) { test = item; } } if (list_Downing.Items.Contains(test)) { ((DownloadManage.HandleModel)list_Downing.Items[list_Downing.Items.IndexOf(test)]).Size = op.Progress.BytesReceived.ToString(); ((DownloadManage.HandleModel)list_Downing.Items[list_Downing.Items.IndexOf(test)]).Status = op.Progress.Status.ToString(); ((DownloadManage.HandleModel)list_Downing.Items[list_Downing.Items.IndexOf(test)]).Progress = ((double)op.Progress.BytesReceived / op.Progress.TotalBytesToReceive) * 100; } } catch (Exception) { return; } }