【问题标题】:How to make a file download progress bar in WinForms?如何在 WinForms 中制作文件下载进度条?
【发布时间】:2019-10-21 18:27:23
【问题描述】:

我正在用 CefSharp 制作一个网络浏览器。我在 2 天前刚刚实现了下载,但下载没有进度条。如何让进度条显示下载进度?

【问题讨论】:

  • 我使用的是 winform 而不是 xaml
  • 您可以更改与此解决方案类似的代码。带有winforms的处理程序和进度条并调用ui

标签: winforms download progress-bar


【解决方案1】:

编辑:让事情更清晰

向您的表单添加一个 ProgressBar 控件,并在您的表单旁边添加一个 BackgroundWorker 组件。首先弄清楚你的文件大小:

Int64 bytes_total= Convert.ToInt64(client.ResponseHeaders["Content-Length"])

这是来自 Alex 的惊人答案的代码,可以在这里找到:

public Form1()
{
    InitializeComponent();

    backgroundWorker1.DoWork += backgroundWorker1_DoWork;
    backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
    backgroundWorker1.WorkerReportsProgress = true;
}

private void button1_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    //Replace this code with a way to figure out how much of the file you have already downloaded and then use backgroundworker1.ReportProgress and send the percentage completion of the file download.
    for (int i = 0; i < 100; i++)
    {
        Thread.Sleep(1000);
        backgroundWorker1.ReportProgress(i);
    }
}

private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

【讨论】:

  • 当我把它放进去时,我得到的错误是“名称“线程”在当前上下文中不存在”和“名称“BackgroundWorker1_ProgressChanged”在当前上下文中不存在”
  • 当我输入 Int64 bytes_total= Convert.ToInt64(client.ResponseHeaders["Content-Length"]) 时,我收到一条错误消息,提示当前上下文中不存在客户端
  • 编辑你的问题并添加更多细节,否则缺乏细节是荒谬的。
猜你喜欢
  • 2019-11-09
  • 2011-01-17
  • 2010-10-12
  • 2013-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 2011-04-19
相关资源
最近更新 更多