Windows 8 BackgroundDownloader(Windows 8)中的进度条

Windows 8 BackgroundDownloader(Windows 8)中的进度条,windows-8,microsoft-metro,progress-bar,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

我使用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...";
        }
    }
private async void btnDownload\u单击(对象发送方,路由目标)
{
Uri源;
存储文件目的文件;
存储文件夹;
string destination=“SampleImage.png”;
if(!Uri.TryCreate(txtUrl.Text.Trim(),UriKind.Absolute,out-source))
{
txtUrl.Text=“请提供正确的URL…”;
返回;
}
尝试
{
folder=等待ApplicationData.Current.LocalFolder.CreateFolderAsync(“SampleFolder”,CreationCollisionOption.OpenIfExists);
destinationFile=wait folder.CreateFileAsync(目标,CreationCollisionOption.GenerateUniqueName);
}
抓住
{
txtProgress.Text=“选择出错的地方…再试一次…”;
返回;
}
BackgroundDownloader downloader=新的BackgroundDownloader();
DownloadOperation download=downloader.CreateDownload(源,目标文件);
if(下载!=null)
{
尝试
{
var progress=新进度(ProgressCallback);//用于显示进度
等待下载.StartAsync().AsTask(cancelProcess.Token,progress);
}
捕获(TaskCanceledException)
{
txtProgress.Text=“已取消”;
}
捕获(例外)
{
txtProgress.Text=“出现问题,请重试…”;
}
}
}
//为了显示进步
私有void ProgressCallback(下载操作obj)
{
双进度=0;
如果(obj.Progress.BytesReceived>0)
{
progress=obj.progress.bytesserved*100/obj.progress.TotalBytesToReceive;
如果(进度>0)
{
txtProgress.Text=string.Format(“下载您的文件….{0}%”,进度);
pbDownloading.Value=progress;//传递进度条值
}
}
其他的
{
txtProgress.Text=“检查您的互联网连接…”;
}
}
我怎样才能得到每一个进度%的下载与此。。。?或者任何其他最好的方法来实现这一点…?

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

首先,您需要设置定时器来检查下载状态。计时器频率大约为每秒10个滴答声,这就是更新下载进度的速度。下载处理程序应更新内部变量
int DownloadPercent
,并以每毫秒百分比为单位测量下载速度:
double DownloadSpeed=DownloadPercent/(DateTime.Now-DownloadStartTime).total毫秒
然后,Dispatchermer callback将每秒检查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();
    }
}
DateTime上次更新//类变量,在下载开始且UI设置为0%时初始化
int显示百分比;
void nextPercent(对象发送方、对象参数){
如果(DisplayedPercent==DownloadPercent)返回;
double uiUpdateSpeed=(DateTime.Now-lastUIUpdate).TotalMillicons/(DisplayedPercent+1);
if(uiUpdateSpeed<下载速度){
下一个百分比();
}
}

我相信这将需要一些调整,但你应该得到的想法。祝你好运

那么,有什么问题,所有下载都使用相同的txtProgress,并且疯狂地交换其中的文本?@alxx我得到的是单个文件下载。。不适用于多个文件。。。txtProgress是一个文本块而不是文本框。那么,又有什么问题呢?我在进度处理程序中看到了硬编码id(txtProgress)。如果此处理程序被多次使用,文本框将随着不同的进程闪烁。问题是,我需要控制downloader的TotalByTestOreReceive。下载程序必须使用我设置的相同字节工作……对不起,我停下来完全理解你。具体来说,你想完成什么?