[英]Multiple download with Background Downloader in windows phone
[英]Issues with background downloader Windows Phone 8.1
我在使用Background Downloader下载多个文件时遇到问题。 我的HandleDownloadAsync
方法中出现“对象未设置为对象实例”错误。 这是我的代码。
private async Task StartDownload(List<DownloadData> data)
{
foreach (DownloadData song in data)
{
Uri source = new Uri(song.downloadUrl);
// Create folder stucture
StorageFolder artistFolder = await KnownFolders.MusicLibrary.CreateFolderAsync(song.artistName, CreationCollisionOption.OpenIfExists);
StorageFolder releaseFolder = await artistFolder.CreateFolderAsync(song.releaseName, CreationCollisionOption.OpenIfExists);
// Create file
StorageFile destinationFile;
try
{
destinationFile = await releaseFolder.CreateFileAsync(song.fileName, CreationCollisionOption.GenerateUniqueName);
}
catch
{
throw;
}
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
List<DownloadOperation> requestOperations = new List<DownloadOperation>();
requestOperations.Add(download);
await HandleDownloadAsync(download, true);
}
}
和方法
private async Task HandleDownloadAsync(DownloadOperation download, bool start)
{
try
{
// Store the download for pause/resume
activeDownloads.Add(download); // Error occurs here
Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
if (start)
{
await download.StartAsync().AsTask(cts.Token, progressCallback);
}
else
{
await download.AttachAsync().AsTask(cts.Token, progressCallback);
}
}
catch
{
throw;
}
finally
{
activeDownloads.Remove(download);
}
}
尝试将下载添加到activeDownloads
时抛出该错误。 大多数代码来自此 MSDN示例,但是我添加了foreach循环来下载多个项目。
似乎您可能忘记了在StartDownload方法中初始化activeDownloads列表,例如:
activeDownloads = new List<DownloadOperation>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.