使用 Caliburn Micro 更新进度条
我一直在阅读 Caliburn 在线程封送方面的出色表现以及更新 UI 线程如何非常简单,但我似乎无法让它工作,也找不到显示它的示例,所以我需要帮助...
这是我的 XAML ...
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<Label Content="Progress For Current File" />
<WrapPanel>
<ProgressBar x:Name="CurrentFileProgress" Width="500" Height="25" Minimum="0" Maximum="1" />
<Label Content="1 of 25" />
</WrapPanel>
</StackPanel>
这是我的 ViewModel...
public void Go()
{
var files = Directory.GetFiles(PathToFiles, "*.sql");
CurrentFileProgress = 0;
for (int iter = 1; iter <= files.Length; iter++)
{
CurrentFileProgress = iter / (files.Length * 1.0f);
System.Threading.Thread.Sleep(250);
}
}
private double _CurrentFileProgress = 3; //For design time look
public double CurrentFileProgress
{
get { return _CurrentFileProgress; }
set
{
_CurrentFileProgress = value;
NotifyOfPropertyChange(() => CurrentFileProgress);
}
}
这很简单,几秒钟后我的进度条达到 100%,但它并不像我预期的那样。
I keep reading how great Caliburn is at thread marshaling and how updating the UI thread is incredibly easy but I can't seem to get it to work nor can I find an example that shows it so I need help...
Here is my XAML...
<StackPanel Orientation="Vertical" HorizontalAlignment="Left">
<Label Content="Progress For Current File" />
<WrapPanel>
<ProgressBar x:Name="CurrentFileProgress" Width="500" Height="25" Minimum="0" Maximum="1" />
<Label Content="1 of 25" />
</WrapPanel>
</StackPanel>
Here is my ViewModel...
public void Go()
{
var files = Directory.GetFiles(PathToFiles, "*.sql");
CurrentFileProgress = 0;
for (int iter = 1; iter <= files.Length; iter++)
{
CurrentFileProgress = iter / (files.Length * 1.0f);
System.Threading.Thread.Sleep(250);
}
}
private double _CurrentFileProgress = 3; //For design time look
public double CurrentFileProgress
{
get { return _CurrentFileProgress; }
set
{
_CurrentFileProgress = value;
NotifyOfPropertyChange(() => CurrentFileProgress);
}
}
This is simple enough and after a few seconds my progress bar is at 100% but it doesn't step like I would expect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜问题是通过调用 Thread.Sleep 你会阻塞 UI 线程,因此在操作完成之前 UI 无法更新。
尝试在后台线程上运行该进程(或使用 TPL - 任务)。
I guess the problem is that by calling Thread.Sleep you block the UI thread so the UI cannot be updated until the action is finished.
Try to run the process on a background thread (or use TPL - Tasks).