在 WPF 中将不确定的进度条与 Backgroundworker 一起使用

Using Indeterminate Progressbar with Backgroundworker in WPF

我创建了一个小工具,用于将数据从一个数据库导入和转换到另一个数据库。由于这需要很多时间,我想用不确定的进度条为用户显示某种进度。 只要导入是 运行,进度条也应该是 运行。 我将此站点用作参考:The ProgressBar control 但我无法理解 DoWork-Method 的作用,因为不确定进度条的选项设置了一次。

我想出了一个半途而废的解决方案,导入完成后我无法停止动画。 有人可以帮我吗?

XAML:

 <ProgressBar Minimum="{Binding Path=Minimum}" Maximum="100" IsIndeterminate="{Binding Path=IsIndeterminate}" Height="20" Grid.Column="2" Grid.Row="5" Grid.ColumnSpan="2"/>

视图模型:

public void StartImportThread()
    {
        ButtonsEnabled = false;
        var th = new Thread(new ThreadStart(StartImport));
        th.IsBackground = true;
        th.Start();
    }
public void StartImport()
    {
        _threadDispatcher = Dispatcher.CurrentDispatcher;
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.WorkerSupportsCancellation = true;
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_Complete;

        worker.RunWorkerAsync();

        // Doing Import Stuff
        worker.CancelAsync(); // Import finished
        ButtonsEnabled=true;
    }
    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
       // while(true)
     //  {
          Minimum = 0;
          IsIndeterminate = true;
    //   }


    }


    private void worker_Complete(object sender, RunWorkerCompletedEventArgs e)
    {
       // if (e.Cancelled)
       // {
            IsIndeterminate = false;
            Minimum = 100;  
       // }

    }

以下是适合我的方法。 RelayCommand 和 ViewModelBase 来自 MvvmLight 库,DoWorkCommand 通过按钮调用:

        <Button Name="BtnReload" Command="{Binding DoWorkCommand}" Width="75" Height="25"/>
        <ProgressBar Minimum="{Binding Path=Minimum}" Maximum="100" IsIndeterminate="{Binding Path=IsIndeterminate}" Height="20"/>



   public class MainViewModel : ViewModelBase {

    private RelayCommand _doWorkCommand;
    private bool _isIndeterminate;
    private int _minimum;
    private Dispatcher _threadDispatcher;


    public int Minimum {
        get { return _minimum; }
        set {
            _minimum = value;
            RaisePropertyChanged(nameof(Minimum));
        }
    }

    public bool IsIndeterminate {
        get { return _isIndeterminate; }
        set {
            _isIndeterminate = value;
            RaisePropertyChanged(nameof(IsIndeterminate));
        }
    }

    public MainViewModel(IDataService dataService) {

    }

    public void StartImportThread() {
        //  ButtonsEnabled = false;
        var th = new Thread(new ThreadStart(StartImport));
        th.IsBackground = true;
        th.Start();
    }

    public void StartImport() {
        _threadDispatcher = Dispatcher.CurrentDispatcher;
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.WorkerSupportsCancellation = true;
        worker.DoWork += worker_DoWork;
        worker.RunWorkerCompleted += worker_Complete;

        worker.RunWorkerAsync();

        // Doing Import Stuff
        worker.CancelAsync(); // Import finished
        //   ButtonsEnabled = true;

    }

    void worker_DoWork(object sender, DoWorkEventArgs e) {
        Minimum = 0;
        IsIndeterminate = true;
        //for (int i = 0; i < 30; i++) {
        //    Thread.Sleep(100);
        //}     
    }

    void worker_Complete(object sender, RunWorkerCompletedEventArgs e) {
        IsIndeterminate = false;
        Minimum = 100;
    }

    public RelayCommand DoWorkCommand => _doWorkCommand ??
        (_doWorkCommand = new RelayCommand(() => StartImportThread()));

}