简体   繁体   English

使用带有 MVVM 模式的 WPF 实现进度条(无 BackgroundWorker)

[英]Implementing Progress bar using WPF with MVVM pattern (without BackgroundWorker)

I have a WPF application which is built on the MVVM design pattern.我有一个基于 MVVM 设计模式的 WPF 应用程序。

I wish to implement a progress bar in the app, that follows the MVVM pattern.我希望在应用程序中实现一个遵循 MVVM 模式的进度条。

I found some solutions to implement this using background-worker class.我找到了一些使用 background-worker 类来实现这个的解决方案。 For some reasons i am not allowable to use background worker.由于某些原因,我不允许使用后台工作人员。

Does any one have any suggestions on how to implement this?有没有人对如何实现这一点有任何建议?

Thanks in advance提前致谢

I would prefer Task over background workers.我更喜欢Task不是后台工作人员。 Anyway, here's a simple implementation of progress bar.不管怎样,这里有一个简单的进度条实现。

<ProgressBar Value="{Binding ProgressValue}" Maximum="100" Height="20" />

public MainViewModel()
{
    Task.Run(() =>
        {
            while (true)
            {
                ProgressValue++;
                Thread.Sleep(250);
            }
        });
    }

    public int ProgressValue
    {
        get { return _progressValue; }
        set { _progressValue = value; OnPropertyChanged(); }
    }
}

The basics are very simple.基础知识非常简单。

View:看法:

 <ProgressBar Value="{Binding ProgressValue}" />

ViewModel:视图模型:

 public double ProgressValue { get/set with INPC }

But the remaining challenge is to set the ViewModels ProgressValue from a non-blocking process in a thread-safe manner.但剩下的挑战是以线程安全的方式从非阻塞进程设置 ViewModels ProgressValue。

Your question lacks the details to help with that.您的问题缺少帮助解决此问题的详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM