简体   繁体   中英

How to force invalidate WPF window?

I'm working on a WPF app that also uses some native C++ proprietary library using DirectX . In some rare but deterministic cases that native library messes up the WPF Window . I need a way to re-render the WPF window even though nothing is changed in WPF point of view. It's just that library (that is out of my control) that has a bug, messing up normal WPF rendering.

I've tried methods like InvalidateVisual , InvalidateArrange , InvalidateMeasure , UpdateLayout . 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.

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.

Is there a way to tell WPF that even though nothing is changed, please redraw anyway?

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.

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. To force a synchronous operation, you can use the Dispatcher to wait for a redraw:

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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