ElementHost - 调用 Show 方法时出现黑色背景 [英] ElementHost - Black Background when calling Show method

查看:44
本文介绍了ElementHost - 调用 Show 方法时出现黑色背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我会说我在谷歌上搜索了这个问题,关于这个问题有很多话题,但没有一个能帮助我解决我的问题(例如:这篇文章或者这个或者这个)

For start I will say that I googled this issue, and there are a lot of topic about that, but none of those helped me to solved my problem (For example: ThisPost, Or This, Or this)

所以问题是我有一个 Windows 窗体应用程序,它承载了一个 WPF 应用程序.我有一个 ElementHost 对象,其中包含作为子项的 WPF 控件.当我使用.Show方法时,我有黑色闪烁1或2秒,然后我看到我的wpf的内容.

So the problem is that I have a Windows Form application, that host a WPF application. I have an ElementHost object, that contains the WPF control as a child. when I use the .Show method, I have black flicker for 1 or 2 second, and then I see the content of my wpf.

我尝试使用可见性或放置不同的背景,或之前设置空白边界,但这些都没有帮助我.如果有人有不同的解决方案,请与我分享.

I try to play with the visibility or to put different background, or set empty bounds before, but none of those helped me. if anyone have different solution, please share it with me.

推荐答案

我遇到了和你一样的问题,我能找到的其他答案都没有实际工作,所以经过长时间的故障排除.我找到了答案.

I had the same issue as you and none of the other answers I could find actually worked and so after a long time of troubleshooting the issue. I found an answer.

如果您构建一个从 Element Host 扩展并在初始构造函数中的类.您可以为宿主容器设置加载事件.Host Container 是显示 Element Hosts Child 的面板.从那里,只需将宿主容器背景颜色设置为元素宿主父背景颜色.

If you build a class extending from Element Host and in the initial constructor. You can set a Load Event for the Host Container. The Host Container is the panel that the Element Hosts Child is being displayed on top of. From there, just set the Host Containers background color to being of the Element Hosts Parents background color.

像这样

using System.Windows;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Media;
public class MyElementHost : ElementHost
{
   public MyElementHost()
    {
        this.HostContainer.Loaded += new RoutedEventHandler(HostPanelLoad);
    }

    public void HostPanelLoad(object sender, RoutedEventArgs e)
    {
        System.Drawing.Color parentColor = this.Parent.BackColor;
        this.HostContainer.Background = new SolidColorBrush(Color.FromArgb(parentColor.A, parentColor.R, parentColor.G, parentColor.B));
    }
}

这篇关于ElementHost - 调用 Show 方法时出现黑色背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆