【发布时间】:2015-08-21 18:48:55
【问题描述】:
我正在制作一个 WPF 应用程序,我使用WebClient 下载文件。我想在ProgressBar 控制器中显示ProgressPercentage。我在使用WebClient 下载文件的类中有一个方法。我的问题是如何将ProgressBar 数据绑定到e.ProgressPercentage。
类中的方法(DownloadFile):
public async Task DownloadProtocol(string address, string location)
{
Uri Uri = new Uri(address);
using (WebClient client = new WebClient())
{
//client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
//client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
client.DownloadProgressChanged += (o, e) =>
{
Console.WriteLine(e.BytesReceived + " " + e.ProgressPercentage);
//ProgressBar = e.ProgressPercentage???
};
client.DownloadFileCompleted += (o, e) =>
{
if (e.Cancelled == true)
{
Console.WriteLine("Download has been canceled.");
}
else
{
Console.WriteLine("Download completed!");
}
};
await client.DownloadFileTaskAsync(Uri, location);
}
}
进度条:
<ProgressBar HorizontalAlignment="Left" Height="24" Margin="130,127,0,0" VerticalAlignment="Top" Width="249"/>
【问题讨论】:
-
ProgressBar.Value = e.ProgressPercentage? -
我无法在其他班级访问我的 ProgressBar? @PhilippeParé
-
您可以创建一个偶数(即“公共事件 ProgressChangedEventHandler ProgressChanged”),并在每次 DownloadProgressChanged 触发时引发它。然后让进度条控制器订阅该事件。
-
感谢您的回复@Dietz,但您不能举个例子吗?我对 WPF 很陌生。谢谢。
标签: c# wpf data-binding progress-bar