C# wpfc中的进度条#
有谁能帮我在我的方法中集成进度条吗?我的意思是,当我执行一个提取zip文件的方法示例时,我会显示一个进度条,以了解我的方法的进度,当方法完成时,进度条将达到100% 这是我提取文件的代码,我想在其中添加一个进度条C# wpfc中的进度条#,c#,wpf,progress-bar,C#,Wpf,Progress Bar,有谁能帮我在我的方法中集成进度条吗?我的意思是,当我执行一个提取zip文件的方法示例时,我会显示一个进度条,以了解我的方法的进度,当方法完成时,进度条将达到100% 这是我提取文件的代码,我想在其中添加一个进度条 public void ExtractFiles() { foreach (var file in d.GetFiles("*.")) { if (!Directory.Exists(targetPath)) Dire
public void ExtractFiles()
{
foreach (var file in d.GetFiles("*."))
{
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
if (!File.Exists(targetPath + file.Name))
{
Directory.Move(file.FullName, targetPath + file.Name);
}
else
{
File.Delete(targetPath + file.Name);
Directory.Move(file.FullName, targetPath + file.Name);
}
}
}
我想知道如何根据我的方法的进度显示进度条的百分比。既然您有相同的格式,它应该是非常直接的 假设您已将ProgressBar命名为pb,您可以尝试以下操作
public void ExtractFiles()
{
var total = d.GetFiles("*.").count();
var progressChange = 100/total;
foreach (var file in d.GetFiles("*."))
{
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
if (!File.Exists(targetPath + file.Name))
{
Directory.Move(file.FullName, targetPath + file.Name);
}
else
{
File.Delete(targetPath + file.Name);
Directory.Move(file.FullName, targetPath + file.Name);
}
pb.Value += progressChange;
}
}
从@bit复制了代码,并对其进行了少量更改 试试这个:
public void ExtractFiles()
{
var files = d.GetFiles("*.");
int total = files.Count();
var progressChange = 100/total;
foreach (var file in files)
{
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
if (!File.Exists(targetPath + file.Name))
{
Directory.Move(file.FullName, targetPath + file.Name);
}
else
{
File.Delete(targetPath + file.Name);
Directory.Move(file.FullName, targetPath + file.Name);
}
//Here is the little bit change I did.
pb.Dispatcher.BeginInvoke(new Action(()=>{
pb.Value += progressChange;
}));
}
}
我希望这能有所帮助。到目前为止,您对进度条做了哪些探索和尝试?您在问什么?如何创建进度条?如何将进度条绑定到值?如何计算该值?如何使用调度程序?@bit,我试着使用一个简单的进度条,即使我的进程已经完成,也能让进程继续前进。我不知道如何将其与我的方法集成我知道你可以使用一个简单的基于事件的机制来更新进度条。。ProgressBar和ExtractFiles()在不同的类中吗?@Bit,我怎么做?等等,我试试这个。谢谢。它确实有用。我只想添加一些修改。它没有填满酒吧