Ich habe einige Dateien, die ich beim Start der App herunterladen muss (erster Lauf). Ich bin mit dem Background Downloader in Windows 8. Dies ist, wie ich es verwenden:Hintergrund Downloader Windows 8 Mehrere Dateien
BackgroundDownloader downloader = new BackgroundDownloader();
List<DownloadOperation> operations = new List<DownloadOperation>();
foreach (FileInfo info in infoFiles)
{
Windows.Storage.ApplicationData.Current.LocalFolder;
foreach (string folder in info.Folders)
{
currentFolder = await currentFolder.CreateFolderAsync(folder, CreationCollisionOption.OpenIfExists);
}
//folder hierarchy created, save the file
StorageFile file = await currentFolder.CreateFileAsync(info.FileName, CreationCollisionOption.ReplaceExisting);
DownloadOperation op = downloader.CreateDownload(new Uri(info.Url), file);
activeDownloads.Add(op);
operations.Add(op);
}
foreach (DownloadOperation download in operations)
{
//start downloads
await HandleDownloadAsync(download, true);
}
Wenn ich versuche, BackgroundDownloader.GetCurrentDownloadsAsync();
zu verwenden, erhalte ich nur einen Hintergrund herunterladen entdeckt. Also habe ich oben den Operator abwarten entfernt, um sie asynchron zu starten.
Allerdings muss ich wissen, wenn alle Dateien fertig sind, damit ich einen Fortschrittsbalken einstellen kann.
Ich brauche eine Möglichkeit, mehrere Dateien herunterzuladen, entdecken Sie alle in BackgroundDownloader.GetCurrentDownloadsAsync();
und wissen, wenn alle Downloads abgeschlossen sind.
private async Task HandleDownloadAsync(DownloadOperation download, bool start)
{
try
{
Debug.WriteLine("Running: " + download.Guid, NotifyType.StatusMessage);
// Store the download so we can pause/resume.
activeDownloads.Add(download);
Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(DownloadProgress);
if (start)
{
// Start the download and attach a progress handler.
await download.StartAsync().AsTask(cts.Token, progressCallback);
}
else
{
// The download was already running when the application started, re-attach the progress handler.
await download.AttachAsync().AsTask(cts.Token, progressCallback);
}
ResponseInformation response = download.GetResponseInformation();
Debug.WriteLine(String.Format("Completed: {0}, Status Code: {1}", download.Guid, response.StatusCode),
NotifyType.StatusMessage);
}
catch (TaskCanceledException)
{
Debug.WriteLine("Canceled: " + download.Guid, NotifyType.StatusMessage);
}
catch (Exception ex)
{
if (!IsExceptionHandled("Execution error", ex, download))
{
throw;
}
}
finally
{
activeDownloads.Remove(download);
}
}
Haben Sie betrachtet [Quickstart: Das Herunterladen einer Datei] (http://msdn.microsoft.com/en-us/library/windows/apps/jj152726.aspx) – Harrison