[英]Progress bar in wpf c#
誰能幫助我如何在一種方法中集成進度條? 我的意思是,當我執行一個方法示例以提取zip文件時,我將顯示一個進度條,以了解我的方法的進度,並且當該方法完成后,進度條將達到100%。
這是我提取文件的代碼,我想在其中添加進度條
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;
}));
}
}
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.