简体   繁体   English

如何强制使 WPF window 无效?

[英]How to force invalidate WPF window?

I'm working on a WPF app that also uses some native C++ proprietary library using DirectX .我正在开发一个 WPF 应用程序,该应用程序还使用一些使用DirectX的本机 C++ 专有库。 In some rare but deterministic cases that native library messes up the WPF Window .在一些罕见但确定性的情况下,本机库会弄乱 WPF Window I need a way to re-render the WPF window even though nothing is changed in WPF point of view.我需要一种重新渲染 WPF window 的方法,即使 WPF 的观点没有任何改变。 It's just that library (that is out of my control) that has a bug, messing up normal WPF rendering.只是那个库(这是我无法控制的)有一个错误,弄乱了正常的 WPF 渲染。

I've tried methods like InvalidateVisual , InvalidateArrange , InvalidateMeasure , UpdateLayout .我尝试过InvalidateVisualInvalidateArrangeInvalidateMeasureUpdateLayout等方法。 Even all those, but it doesn't work.即使所有这些,但它不起作用。 One way of making it partially work is to change some property (like changing Foreground brush color by one bit) in one of the various controls in the window, but then the re-render is partial only.使其部分工作的一种方法是在 window 中的各种控件之一中更改某些属性(例如将Foreground画笔颜色更改一位),但是重新渲染只是部分的。

One certain way is resize the window a bit, but that doesn't work when the app is in full screen mode that we support, in addition to the standard maximized mode.一种确定的方法是稍微调整 window 的大小,但是当应用程序处于我们支持的全屏模式时,除了标准的最大化模式外,这不起作用。

Is there a way to tell WPF that even though nothing is changed, please redraw anyway?有没有办法告诉 WPF 即使什么都没改变,还是请重画?

UPDATE: The fix in the proprietary library was provided, curing the problem for me.更新:提供了专有库中的修复程序,为我解决了问题。 But still it's strange that there is no good way to force repaint.但是仍然很奇怪,没有强制重绘的好方法。 The best workaround was to traverse all the visual children and modify FG and BG brushes just a little bit, forcing the redraw.最好的解决方法是遍历所有视觉子对象并稍微修改 FG 和 BG 画笔,强制重绘。

I think InvalidateVisual is the way to go, but as you may not know the rendering in WPF is performed on another thread so the redraw will not be performed synchronously.我认为InvalidateVisual是 go 的方式,但您可能不知道 WPF 中的渲染是在另一个线程上执行的,因此不会同步执行重绘。 To force a synchronous operation, you can use the Dispatcher to wait for a redraw:要强制进行同步操作,可以使用Dispatcher等待重绘:

// In your Window code-behind
this.InvalidateVisual();
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => { })).Wait();
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => { })).Wait();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM