2013-02-04 69 views
2

我有一個應用程序,其中我具有結合一個數據網格女巫是在標籤項目的用戶控制。WPF運行動畫在單獨的線程

這個過程需要一段時間,所以我在單獨的線程創建加載動畫另一個標籤項目,當我在加載數據網格數據I與動畫選擇的標籤項,直到數據被加載。

的問題是,在動畫開始,但是當數據網格結合開始時,動畫凍結。

this.Dispatcher.Invoke(DispatcherPriority.Normal, 
      new Action(
       delegate() 
       { 
        tiLoading.Content = new Controls.Loading.LoadingAnimation(); 
        tiLoading.IsSelected = true; 
       } 
     )); 

//And now I populate the content of the Tab Item with the User Control that contains data grid 

connType = choseConnType(); // Here is a dialog witch ask the user somethig and return a value 
tiGeneral.Content = new Windows.General(true, connType); 

在對話框啓動綁定之後,LoadingAnimation會凍結。

回答

0

這是可以理解的。如果您有長時間運行的流程,您應該使用BackgroundWorker來運行它。

public void ItsGonnaBeLong() 
{ 
    this.IsBusy = true; 
    var worker = new BackgroundWorker(); 
    worker.DoWork += this.MyDoWorkMethod; 
    worker.RunWorkerCompleted += this.MyDoWorkCompleted; 
    worker.RunWorkerAsync(); 
} 

private void MyDoWorkCompleted(object sender, RunWorkerCompletedEventArgs e) 
{ 
    // executed after work on background thread is completed 
    this.IsBusy = false; 
} 

private void MyDoWorkMethod(object sender, DoWorkEventArgs e) 
{ 
    // Do something. 
} 

是不是讓你的動畫在不同的TabItem?你有沒有考慮使用從Extended WPF ToolkitBusyIndicator?您可以簡單地使用BusyIndi​​cator包裝Control並使用其IsBusy屬性來運行它。

<xctk:BusyIndicator IsBusy="True" > 
     <my:MyUserControl /> 
</xctk:BusyIndicator> 
+1

謝謝,我會嘗試使用BusyIndi​​cator,但我不能使用BackgroundWorker,因爲它必須是STA線程。 – Gabriel

+0

@ user2039717好吧,我不明白爲什麼......你不應該做'BackgroundWroker'動畫。你應該在後臺加載這些DataGrid的項目...沒有理由爲什麼它應該在STA線程上。我認爲你從錯誤的角度看待你的問題。 –