MVVM进度条和后台工作者可见性

时间:2012-12-13 13:41:42

标签: c# wpf mvvm progress-bar backgroundworker

我正在使用WPF MVVM并有一个进度条来报告任务的进度,使用后台工作程序。如果我在工作完成时使进度条折叠,我会遇到问题,如果我这样做,进度条永远不会结束。如果我看到进度条可见,那很好,但我不希望任务完成后进度条可见。谁能告诉我哪里出错了?在下面的GenerateOutput只是一个创建一些文件并随着时间推移更新进度的函数。 ShowSummary()是任务完成时需要运行的东西。我尝试在RunWorkerCompleted中放置一个Thread.Sleep,这样用户可以在折叠之前实际看到完成的栏,但是你仍然看不到它已完成。

public void BackgroundWorkerGenerateOutputProgress()
    {
        ProgressVisibility = Visibility.Visible;
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += new DoWorkEventHandler(DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
        worker.RunWorkerAsync();
    }

void DoWork(object sender, DoWorkEventArgs e)
    {
        if (IsInProgress)
            return;
        CurrentProgress = 0;
        IsInProgress = true;
        BackgroundWorker worker = sender as BackgroundWorker;
        GenerateOutput();
    }
void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        IsInProgress = false;
        ProgressVisibility = Visibility.Collapsed;
        ShowSummary();
    }
编辑:我已将其更改为以下内容,我仍然没有看到进度条结束:

public void BackgroundWorkerGenerateOutputProgress()
    {
        ProgressVisibility = Visibility.Visible;
        CurrentProgress = 0;
        BackgroundWorker worker = new BackgroundWorker();
        worker.WorkerReportsProgress = true;
        worker.DoWork += new DoWorkEventHandler(DoWork);
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(RunWorkerCompleted);
        worker.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
        worker.RunWorkerAsync();
    }

    private void ProgressChanged(object sender,
        ProgressChangedEventArgs e)
    {
        CurrentProgress = e.ProgressPercentage;
    }
 void DoWork(object sender, DoWorkEventArgs e)
    {
        if (IsInProgress)
            return;
        IsInProgress = true;
        BackgroundWorker worker = sender as BackgroundWorker;

        DoIt(worker);
    }
    void DoIt(BackgroundWorker worker)
    {
        worker.ReportProgress(20);
        Thread.Sleep(1000);
        worker.ReportProgress(40);
        Thread.Sleep(1000);
        worker.ReportProgress(60);
        Thread.Sleep(1000);
        worker.ReportProgress(80);
        Thread.Sleep(1000);
        worker.ReportProgress(100);

    }
    void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        IsInProgress = false;
        ShowSummary();
        System.Threading.Thread.Sleep(2000);
        ProgressVisibility = Visibility.Collapsed;
    }

xaml(抱歉无法格式化):

<ProgressBar HorizontalAlignment="Left" Value="{Binding CurrentProgress, Mode=OneWay}" Height="20" Width="200" Visibility="{Binding Path=ProgressVisibility}" Minimum="0" Maximum="100" />

2 个答案:

答案 0 :(得分:1)

尝试使用此链接中显示的ReportProgress。你没有WorkerReportsProgress = true

BackgroundWorker Class

如果您在GenerateOutput()的末尾设置它,则将其设置为Visibility.Collapsed;在呈现UI之前。

答案 1 :(得分:0)

我没有试过这个,但逻辑上认为我会采用这种方法。在隐藏进度值之前,将进度值设置为maxvalue。

void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    CurrentProgress = 0; //Set this to max value
    IsInProgress = false;
    ProgressVisibility = Visibility.Collapsed;
    ShowSummary();
}