2016-02-29 40 views
-1

我在顯示進度欄值時遇到問題。我想,進度條使用其他線程來更新他的值,但它只能在ParseTpSheet方法之後執行。我希望你能幫助我。謝謝。WPF ProgressBar顯示問題

namespace IntegrationPlanning.Controls 
{ 

/// <summary> 
/// Логика взаимодействия для PlaningParserControl.xaml 
/// </summary> 
[Serializable] 
public partial class PlaningParserControl : UserControl 
{ 
    private BackgroundWorker worker = new BackgroundWorker(); 
    private readonly PlaningParserModel _parserModel = new PlaningParserModel(); 
    public PlaningParserControl() 
    { 
     InitializeComponent(); 
     worker.WorkerReportsProgress = true; 
     worker.ProgressChanged += worker_ProgressChanged; 
     worker.DoWork += worker_DoWork; 
    } 

    void worker_DoWork(object sender, DoWorkEventArgs e) 
    { 
     for (var i = 0; i < 100; i++) 
     { 
      Thread.Sleep(150); 
      worker.ReportProgress(i); 
     } 
    } 

    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     ParseProgressBar.Value = e.ProgressPercentage; 
    } 

    private void IncProgressBar(int value) 
    { 
     ParseProgressBar.Value += value; 
    } 

    private void AddFileButton_OnClick(object sender, RoutedEventArgs e) 
    { 
     var excelApplication = new Excel.Application(); 
     var openFileDialog = new OpenFileDialog {Filter = "excel files(*.xls)(*.xlsx)|*.xls;*.xlsx" }; 
     openFileDialog.ShowDialog(); 
     if (openFileDialog.FileName.EndsWith(".xls") || openFileDialog.FileName.EndsWith(".xlsx")) 
     { 
      ParseProgressBar.Value = 0; 
      worker.RunWorkerAsync(); 
      _parserModel.Workbook = excelApplication.Workbooks.Add(openFileDialog.FileName); 
      _parserModel.ParseTpSheet(); 
     } 
     else 
     { 
      MessageBox.Show("Файл не был выбран", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information); 
     } 
     excelApplication.Quit(); 
    } 
    } 
} 
+1

'AddFileButton_OnClick'在UI線程上運行,任何UI更新都必須等到您從中返回。你可以使用'Task.Run'在任務中移動_parserModel東西,這樣UI線程可以在後臺工作者運行時自由更新進度條。 – Haukinger

+0

當UI線程執行'_parserModel.ParseTpSheet();'方法時,你的UI是否凍結? – StepUp

+1

人們爲什麼應該使用數據綁定而不是直接操作UI對象的另一個例子。將進度條的值綁定到包含更改通知的屬性,並且可以從任何您想要的線程更新它。 –

回答

0

非常感謝@Haukinger。結果代碼在這裏:

namespace IntegrationPlanning.Controls 
{ 

/// <summary> 
/// Логика взаимодействия для PlaningParserControl.xaml 
/// </summary> 
[Serializable] 
public partial class PlaningParserControl : UserControl 
{ 
    public int ParseProgressBarValue { get; set; } 
    private BackgroundWorker worker = new BackgroundWorker(); 
    private readonly PlaningParserModel _parserModel = new PlaningParserModel(); 
    public PlaningParserControl() 
    { 
     InitializeComponent(); 
     worker.WorkerReportsProgress = true; 
     worker.ProgressChanged += worker_ProgressChanged; 
     worker.DoWork += worker_DoWork; 
    } 

    void worker_DoWork(object sender, DoWorkEventArgs e) 
    { 
     for (var i = 0; i < 100; i++) 
     { 
      Thread.Sleep(150); 
      worker.ReportProgress(i); 
     } 
    } 

    private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     ParseProgressBar.Value = e.ProgressPercentage;; 
    } 

    private void AddFileButton_OnClick(object sender, RoutedEventArgs e) 
    { 
     var excelApplication = new Excel.Application(); 
     var openFileDialog = new OpenFileDialog {Filter = "excel files(*.xls)(*.xlsx)|*.xls;*.xlsx" }; 
     openFileDialog.ShowDialog(); 
     if (openFileDialog.FileName.EndsWith(".xls") || openFileDialog.FileName.EndsWith(".xlsx")) 
     { 
      ParseProgressBar.Value = 0; 
      worker.RunWorkerAsync(); 
      var t = Task.Run(() => 
      { 
       _parserModel.Workbook = excelApplication.Workbooks.Add(openFileDialog.FileName); 
       _parserModel.ParseTpSheet(); 
      }); 
     } 
     else 
     { 
      MessageBox.Show("Файл не был выбран", "Уведомление", MessageBoxButton.OK, MessageBoxImage.Information); 
     } 
     excelApplication.Quit(); 
    } 
} 
}