简体   繁体   English

Winforms WPF Interop - WPF内容无法绘制

[英]Winforms WPF Interop - WPF content fails to paint

I have a WinForm that uses an ElementHost to display a WPF UserControl. 我有一个使用ElementHost来显示WPF UserControl的WinForm。 Once every 50 times or so when the form loads the WPF content fails to paint. 表单加载时,每50次左右,WPF内容无法绘制。 You can see through the WinForm chrome to whatever is beneath. 您可以通过WinForm chrome查看下面的内容。 Resizing the window gets the WPF content to show up. 调整窗口大小可以显示WPF内容。

Is this a known issue? 这是一个已知的问题? Can anyone suggest a workaround? 任何人都可以建议解决方法?

We have fought these types of issues before. 我们之前已经打过这些类型的问题。 See this WPF forum post for more info on our particular flavor (I don't know if it is the same issue or not). 有关我们特定风味的更多信息,请参阅此WPF论坛帖子 (我不知道它是否是同一个问题)。

The only thing that we found to work was to change the size of the ElementHost . 我们发现唯一可行的方法是改变ElementHost的大小

_elementHost.Width++;

It's a complete hack, ugly, and I'm embarrassed to even post it. 这是一个完整的黑客,丑陋,甚至发布它我都很尴尬。 But nothing else ever worked for us. 但没有别的东西对我们有用。 So, it is definitely a workaround. 所以,这绝对是一种解决方法。 (Grin) (GRIN)

We tried Invalidate, Refresh and everything we could think of ... on the ElementHost. 我们在ElementHost上尝试了Invalidate,Refresh和我们能想到的一切...... We also tried InvalidateMeasure, InvalidateArrange, and InvalidateVisual on the WPF hosted content. 我们还在WPF托管内容上尝试了InvalidateMeasure,InvalidateArrange和InvalidateVisual。 No luck. 没运气。

If you find another way to fix your issue, I would love to hear about it. 如果您找到另一种方法来解决问题,我很乐意听到。

Good luck, I know I have lost some hair on this one. 祝你好运,我知道我已经失去了一些头发。

Update 1: I have submitted another WPF forum post on this. 更新1:我已经提交了另一个WPF论坛帖子 Maybe we can get a response from Microsoft. 也许我们可以得到微软的回复。 Sure seems like a bug to me. 当然对我来说似乎是个错误。

Update 2: After I fixed the refresh issue with the above hack ... I still had another problem to solve that I thought worth mentioning here. 更新2:我用上面的黑客修复了刷新问题......我还有另外一个问题需要解决,我觉得这里值得一提。 That is: there was a definite delay until the screen refreshed. 那就是:在屏幕刷新之前有一定的延迟。 This made it seem like the user was navigating to another screen (it wasn't ... it was just the contents of the double buffering buffer). 这使得用户似乎导航到另一个屏幕(它不是......它只是双缓冲缓冲区的内容)。 I ended up having to manually call System.Windows.Forms.Control.Refresh() on the Control that was hosting the ElementHost. 我最终不得不在托管ElementHost的Control上手动调用System.Windows.Forms.Control.Refresh()。 In this way, even though the pause was still there ... at least the screen was blank ... and it didn't look like the user was navigating somewhere ... 通过这种方式,即使暂停仍然存在...至少屏幕是空白的...并且它看起来不像用户在某处导航...

the following worked for me. 以下为我工作。
On the Form_Activated event, I added the following Form_Activated事件上,我添加了以下内容

elementHost1.HostContainer.InvalidateVisual();

I know this post is old, but in the later version of .net (ie 3.5), you can try to use software rendering mode. 我知道这篇文章很老,但是在更高版本的.net(即3.5)中,你可以尝试使用软件渲染模式。 This seems to fix a lot of problems when you host a WPF control in the ElementHost. 当你在ElementHost中托管WPF控件时,这似乎解决了很多问题。

Ref: Software rendering mode - WPF 参考: 软件渲染模式 - WPF

也许在ElementHost上调用Invalidate?

您是否尝试使用反射来查看_elementHost.Width ++背后的代码?

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

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