在WP8.1中下载多个文件

时间:2015-07-15 04:31:29

标签: windows-phone-8.1

我正在开发一个可以下载多个文件的应用程序,但我一次只能获得单个文件。

我在按钮点击下载文件时实现的代码如下:

private async void ButtonDownload_Click_1(object sender, RoutedEventArgs e)
{

    string uri = "download url****************************";
    StorageFolder folder = ApplicationData.Current.LocalFolder;
    if (folder != null)
    {
        StorageFile file = await folder.CreateFileAsync(++fileNo + "stVideo.mp4", CreationCollisionOption.GenerateUniqueName);
        downloadOperation = backgroundDownloader.CreateDownload(new Uri(uri), file);
        Progress<DownloadOperation> progress = new Progress<DownloadOperation>(progressChanged);
        cancellationToken = new CancellationTokenSource();

        try
        {
            TextBlockStatus.Text = "Initializing...";
            await downloadOperation.StartAsync().AsTask(cancellationToken.Token, progress);
        }
        catch (TaskCanceledException)
        {
            TextBlockStatus.Text = "Download canceled.";
            downloadOperation.ResultFile.DeleteAsync().AsTask().Wait();

            ButtonDownload.IsEnabled = true;
            downloadOperation = null;
        }
    }
}

以及进度更改事件如下:

private void progressChanged(DownloadOperation downloadOperation)
{
    int progress = (int)(100 * ((double)downloadOperation.Progress.BytesReceived / (double)downloadOperation.Progress.TotalBytesToReceive));
    TextBlockProgress.Text = String.Format("{0} of {1} kb. downloaded - %{2} complete.", downloadOperation.Progress.BytesReceived / 1024, downloadOperation.Progress.TotalBytesToReceive / 1024, progress);
    ProgressBarDownload.Value = progress;
    switch (downloadOperation.Progress.Status)
    {
        case BackgroundTransferStatus.Running:
        {
            TextBlockStatus.Text = "Downloading...";
            break;
        }
        case BackgroundTransferStatus.Completed:
        {
            TextBlockStatus.Text = "Download complete.";
            downloadOperation = null;
            break;
        }
        case BackgroundTransferStatus.PausedByApplication:
        {
            TextBlockStatus.Text = "Download paused.";
            break;
        }
        case BackgroundTransferStatus.PausedCostedNetwork:
        {
            TextBlockStatus.Text = "Download paused because of metered connection.";
            break;
        }
        case BackgroundTransferStatus.PausedNoNetwork:
        {
            TextBlockStatus.Text = "No network detected. Please check your internet connection.";
            break;
        }
        case BackgroundTransferStatus.Error:
        {
            TextBlockStatus.Text = "An error occured while downloading.";
            break;
        }
    }
}

但是我无法提出下载多个文件的解决方案。

请详细说明如何摆脱这个问题。

由于

1 个答案:

答案 0 :(得分:0)

如果您点击两次或更多次 - 您使用一个进度处理程序创建多次下载。 当您创建下载操作项时,系统会生成唯一ID - Guid。 您可以在 progressChanged

中确定下载哪个项目

据我所知:

  1. 同一时刻的最大下载操作 - 5

  2. 队列中的最大下载操作 - 500

  3. UPDATE:

    我为后台下载创建了简单的示例。您可以从OneDrive

    下载示例