2015-02-08 84 views
0

我在使用后台下载程序下载多个文件时遇到问题。我在我的HandleDownloadAsync方法中收到“未设置为对象实例的对象”错误。这是我的代码。试图下载添加到activeDownloads时抛出后台下载程序问题Windows Phone 8.1

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); 
     } 
    } 

错误。大多数代码是从This MSDN示例,但我添加了foreach循环来下载多个项目。

回答

0

好像你可能已经忘记了initalize在StartDownload方法activeDownloads列表,例如像这样:

activeDownloads = new List<DownloadOperation>(); 
相关问题