我在使用 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
在 Dispatcher 线程上进行了更新,则此代码可以正常工作,但这对于此特定应用程序来说并不理想。
有什么建议么?