ond*_*vic 3 c# wpf dotnetzip progress-bar
更新:让它工作更新我的工作代码
这是我到目前为止所拥有的
private async void ZipIt(string src, string dest)
{
await Task.Run(() =>
{
using (var zipFile = new ZipFile())
{
// add content to zip here
zipFile.AddDirectory(src);
zipFile.SaveProgress +=
(o, args) =>
{
var percentage = (int)(1.0d / args.TotalBytesToTransfer * args.BytesTransferred * 100.0d);
// report your progress
pbCurrentFile.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
pbCurrentFile.Value = percentage;
}
));
};
zipFile.Save(dest);
}
});
}
Run Code Online (Sandbox Code Playgroud)
我需要弄清楚如何更新我的进度条,但不确定我是否在正确的轨道上我已经搜索过并找到了很多关于Windows窗体和vb.net的例子,但是wpf c#没有想知道是否有人可以提供帮助.
我猜你使用的是DotNetZip?
您展示的代码中存在许多问题:
DoWork那么你期望如何获得进展?zip.Save()你不会得到进展(除了100%),因为它不会返回,除非它完成.解
SaveProgress改为使用任务和事件:
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
await Task.Run(() =>
{
using (var zipFile = new ZipFile())
{
// add content to zip here
zipFile.SaveProgress +=
(o, args) =>
{
var percentage = (int) (1.0d/args.TotalBytesToTransfer*args.BytesTransferred*100.0d);
// report your progress
};
zipFile.Save();
}
});
}
Run Code Online (Sandbox Code Playgroud)
这样,您的UI就不会冻结,您将获得进度的定期报告.
总是喜欢任务,BackgroundWorker因为它是现在使用的官方方法.