【发布时间】:2019-10-21 18:27:23
【问题描述】:
我正在用 CefSharp 制作一个网络浏览器。我在 2 天前刚刚实现了下载,但下载没有进度条。如何让进度条显示下载进度?
【问题讨论】:
-
我使用的是 winform 而不是 xaml
-
您可以更改与此解决方案类似的代码。带有winforms的处理程序和进度条并调用ui
标签: winforms download progress-bar
我正在用 CefSharp 制作一个网络浏览器。我在 2 天前刚刚实现了下载,但下载没有进度条。如何让进度条显示下载进度?
【问题讨论】:
标签: winforms download progress-bar
编辑:让事情更清晰
向您的表单添加一个 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;
}
【讨论】: