我正在制作一个WPF应用程序,在其中使用
类中的方法(DownloadFile):
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"/>
进度条.Value = e.ProgressPercentage
。 - Philippe Paré