1

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

有什么建议么?

4

1 回答 1

1

一些试验和错误让我发现属性更改通知是按DispatcherPriority.Background优先级发送的,因此将刷新行更改为:

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

...看起来它解决了问题。DispatcherPriority.ContextIdle低于一级DispatcherPriority.Backgound。渲染现在每次都会持续更新。

于 2016-06-30T14:50:45.100 回答