2012-12-13 66 views
1

我正在使用WPF MVVM並且有一個進度條來使用後臺工作來報告任務的進度。它可以工作,除非我在進度條在工作完成時摺疊,如果我這樣做,進度條永遠不會結束,那麼我遇到了問題。如果我可以看到進度條沒有問題,但我不希望在任務完成時顯示進度欄。有人能告訴我我要去哪裏嗎?在下面的GenerateOutput只是一個函數,它創建一些文件並更新進度。 ShowSummary()是任務完成時需要運行的東西。我試着在RunWorkerCompleted中放置一個Thread.Sleep,這樣用戶在摺疊前可以看到完成的欄,但仍然看不到它完成。MVVM進度條和後臺工作者可見性

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" /> 
+0

它是如何工作的?我看不到任何對ReportProgress或WorkerReportsProgress = true的調用。 – Paparazzi

+0

在執行每個操作之後,我在GenerateOutput方法中更新CurrentProgress屬性(雙精度型)。 ProgressBar的值綁定到CurrentProgress。我不確定這是正確的方式,因此我的問題,但它確實有效。我在GenerateOutput的末尾將CurrentProgress設置爲100(最大值,最小值爲0)。 – Nix

回答

1

嘗試ReportProgress,如此鏈接所示。你不必WorkerReportsProgress =真

BackgroundWorker Class

如果您在GenerateOutput(),然後將其設置爲Visibility.Collapsed年底將它設置;在UI呈現之前。

+0

看到我的編輯。在崩潰之前,仍然沒有看到進展結束。 – Nix

+0

所以你看到的進步只是不是100%? – Paparazzi

+0

是的,我明白了,但從未達到100%。如果我將可見性設置爲摺疊狀態的線條取出爲100%。 – Nix

0

我還沒有試過這個,但邏輯思維我會做這apporch。在隱藏它之前,將進度值設置爲其最大值。

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