2016-06-30 22 views
1

我遇到了RenderTargetBitmap問題,因爲我無法在後臺線程上更改綁定屬性後始終如一地獲取更新的呈現。WPF確保RenderTargetBitmap已更新後臺線程更改的綁定值

以下是我有:

 // Update a property on an INotifyPropertyChanged view model 
     // This runs on a background thread 
     viewModel.SomeBoundProperty += 10; 

     // Flush dispatcher queue (from http://stackoverflow.com/a/2596035/612510) 
     _lcd.Dispatcher.Invoke(() => {}, System.Windows.Threading.DispatcherPriority.Loaded); 

     // Render the updated control 
     _lcd.Dispatcher.Invoke(() => 
     { 
      _lcd.Measure(new System.Windows.Size(240, 160)); 
      _lcd.Arrange(new System.Windows.Rect(0, 0, 240, 160)); 
      _lcd.UpdateLayout(); 

      _renderTarget.Render(_lcd); 
     } 

可惜的是,約一半的時候,我發現了控制與新值更新之前渲染,而另一半是正確更新。

從我理解的WPF自動調度屬性更改通知到UI線程。我如何確保在執行渲染之前全部處理這些分派的通知?如果我確認SomeBoundProperty已在分派器線程上更新,則此代碼正常工作,但對於此特定應用程序而言,這並不理想。

有什麼建議嗎?

回答

0

一些試驗和錯誤我盤算,屬性更改通知在DispatcherPriority.Background優先級調度,所以改變沖洗管道,以這樣的:

// Flush dispatcher queue 
_lcd.Dispatcher.Invoke(() => {}, System.Windows.Threading.DispatcherPriority.ContextIdle); 

...看起來像它解決了這一問題。 DispatcherPriority.ContextIdleDispatcherPriority.Backgound低一級。渲染現在每次更新一致。