BackgroundDownloader中的进度条(Windows 8)

时间:2012-11-01 08:52:55

标签: windows-8 microsoft-metro progress-bar

我正在使用BackgroundDownloader从URL下载文件。我必须在进度条中显示每个下载百分比(如1%,2%,3%,...)并显示为文本。但是每次下载(只有一个文件)我都会获得大量的下载百分比(如40%,60%......)。这是我的代码:

private async void btnDownload_Click(object sender, RoutedEventArgs e)
    {
        Uri source;
        StorageFile destinationFile;
        StorageFolder folder;
        string destination = "SampleImage.png";
        if (!Uri.TryCreate(txtUrl.Text.Trim(), UriKind.Absolute, out source))
        {
            txtUrl.Text = "Pls provide correct URL...";
            return;
        }
        try
        {
            folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("SampleFolder", CreationCollisionOption.OpenIfExists);
            destinationFile = await folder.CreateFileAsync(destination, CreationCollisionOption.GenerateUniqueName);
        }
        catch
        {
            txtProgress.Text = "Opss something went wrong... try again....";
            return;
        }

        BackgroundDownloader downloader = new BackgroundDownloader();
        DownloadOperation download = downloader.CreateDownload(source, destinationFile);

        if (download != null)
        {
            try
            {
                var progress = new Progress<DownloadOperation>(ProgressCallback); // for showing progress
                await download.StartAsync().AsTask(cancelProcess.Token, progress);
            }
            catch (TaskCanceledException)
            {
                txtProgress.Text = "Canceled";
            }
            catch(Exception)
            {
                txtProgress.Text = "Something went wrong pls try again....";
            }
        }
    }
//for showing progress
private void ProgressCallback(DownloadOperation obj)
    {
        double progress = 0;
        if (obj.Progress.BytesReceived > 0)
        {
            progress = obj.Progress.BytesReceived * 100 / obj.Progress.TotalBytesToReceive;
            if (progress > 0)
            {
                txtProgress.Text = string.Format("Downloading your file.... {0}%", progress);
                pbDownloading.Value = progress; // passing progress bar value
            }
        }
        else
        {
            txtProgress.Text = "Check your internet connection...";
        }
    }

如何通过此获取每次下载的进度%?或者任何其他最佳方式......?

1 个答案:

答案 0 :(得分:2)

因此,您需要平滑更改下载进度(以整数百分比衡量),而不是可能跳跃。然后,您不应该按原样显示原始下载进度,而是创建方法,将显示的进度增加1%(nextPercent)并以与下载速度成比例的频率调用它。

首先,您需要设置计时器以检查下载状态。定时器频率可以是每秒10个滴答,这是下载进度可以更新的速度。下载处理程序应更新内部变量int DownloadPercent并以百万分之一的百分比来衡量下载速度:double DownloadSpeed = DownloadPercent/(DateTime.Now - DownloadStartTime).TotalMilliseconds;
然后,DispatcherTimer回调将每秒检查下载进度10次,如果显示的进度小于实际,则调用nextPercent并且自上次UI更新以来已经过了足够的时间。现在,您如何确定足够的时间:

DateTime lastUIUpdate; //class variable, initialized when download starts and UI is set to 0%
int DisplayedPercent;

void nextPercent(object sender, object args) {
    if (DisplayedPercent == DownloadPercent) return;

    double uiUpdateSpeed = (DateTime.Now - lastUIUpdate).TotalMilliseconds / (DisplayedPercent + 1);
    if (uiUpdateSpeed < DownloadSpeed) {
         nextPercent();
    }
}

我确定这需要一些调整,但你应该明白这个想法。祝你好运!