/// <summary> /// 要下载调用这个方法 /// </summary> /// <param name="url">下载的文件网址的来源</param> /// <returns></returns> public static async Task Start(string filename,string url,DownloadType type,StorageFolder folder=null) { try { Uri uri = new Uri(Uri.EscapeUriString(url), UriKind.RelativeOrAbsolute); BackgroundDownloader downloader = new BackgroundDownloader(); if (folder==null) { folder = await KnownFolders.MusicLibrary.CreateFolderAsync("kgdownload", CreationCollisionOption.OpenIfExists); switch (type) { case DownloadType.song: folder = await folder.CreateFolderAsync("song", CreationCollisionOption.OpenIfExists); downloader.TransferGroup = BackgroundTransferGroup.CreateGroup("song"); break; case DownloadType.mv: folder = await folder.CreateFolderAsync("mv", CreationCollisionOption.OpenIfExists); downloader.TransferGroup = BackgroundTransferGroup.CreateGroup("mv"); break; case DownloadType.other: folder = await folder.CreateFolderAsync("other", CreationCollisionOption.OpenIfExists); downloader.TransferGroup = BackgroundTransferGroup.CreateGroup("other"); break; default: break; } }else { downloader.TransferGroup = BackgroundTransferGroup.CreateGroup("other"); } //string name = uri.ToString().Substring(uri.ToString().LastIndexOf("/"), uri.ToString().Length); string name = filename; StorageFile file = await folder.CreateFileAsync(name, CreationCollisionOption.GenerateUniqueName); downloader.FailureToastNotification = DownloadedToast.Done(filename, DownloadedToast.DownResult.Fa); downloader.SuccessToastNotification = DownloadedToast.Done(filename, DownloadedToast.DownResult.Su); var download = downloader.CreateDownload(new Uri(url), file); TransferModel transfer = new TransferModel(); transfer.DownloadOperation = download; transfer.Source = download.RequestedUri.ToString(); transfer.Destination = download.ResultFile.Path; transfer.BytesReceived = download.Progress.BytesReceived; transfer.TotalBytesToReceive = download.Progress.TotalBytesToReceive; transfer.Progress = 0; transfers.Add(transfer); Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress); download.StartAsync().AsTask(cancelToken.Token, progressCallback); } catch { await new MessageDialog("链接已失效!").ShowAsync(); } }