使用 Caliburn Micro 更新进度条

发布于 2024-12-11 15:33:18 字数 1114 浏览 0 评论 0原文

我一直在阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2024-12-18 15:33:18

我猜问题是通过调用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文