2015-02-08 44 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>();