2

我正在使用 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...";
        }
    }

我怎样才能获得下载的每一个进度百分比......?或任何其他最好的方法来做到这一点......?

4

1 回答 1

2

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

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

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

我确信这需要一些调整,但你应该明白这一点。祝你好运!

于 2012-11-01T12:39:48.307 回答