【发布时间】:2011-08-17 12:27:38
【问题描述】:
我正在为我的 WPF 应用程序添加一个进度条。我想在进度条上显示实时进度以及实时生成文件的数量,例如生成的 4/100 个文件等。下面是两个动作和执行这些动作的任务
Action Generate = new Action(() =>
{
foreach (string file in Files)
{
//all the logic to generate files
using (var streamWriter = new StreamWriter(newFileName, false, Encoding.Default))
{
foreach (string segment in newFile)
{
streamWriter.WriteLine(segment);
}
filesGenerated++;
//I need to do the second action here
}
}
});
Action ShowProgressBar = new Action(() =>
{
progressBar.Value = filesGenerated
lblProgress.Content = filesGenerated + " File(s) Generated.";
});
Task GenerateTask = Task.Factory.StartNew(() => Generate());
Task ShowProgressBarTask = new Task(ShowProgressBar);
我尝试嵌套任务,但它不起作用。我应该怎么做才能在进度条中显示实时进度。
【问题讨论】:
-
请使用扩展的 WPF 工具包,它有 BusyIndicator 元素:Codeplex projectBusyIndicator documentation
-
链接到第三方组件并不能真正帮助解决代码问题。您不需要工具包来显示进度!
-
它与 Silverlight Toolkit 相同,是构建 Silverlight 应用程序的重要组成部分,就像这个 WPF Toolkit...