1

我正在使用 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" />
4

2 回答 2

1

尝试 ReportProgress,如此链接所示。你没有 WorkerReportsProgress = true

BackgroundWorker 类

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

于 2012-12-13T14:32:10.123 回答
0

我没有尝试过,但逻辑上认为我会这样做。在隐藏之前将进度值设置为其最大值。

void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    CurrentProgress = 0; //Set this to max value
    IsInProgress = false;
    ProgressVisibility = Visibility.Collapsed;
    ShowSummary();
}
于 2012-12-13T13:44:23.970 回答