【问题标题】:Black background before loading a wpf controll when using ElementHost使用 ElementHost 时加载 wpf 控件之前的黑色背景
【发布时间】:2011-01-06 10:36:18
【问题描述】:

我在带有 ElementHost 的 WinForms 中使用 WPF。当表单加载时,ElementHost 即将加载的地方会出现一闪而过的黑色背景。这看起来有点糟糕。有关如何摆脱这种情况的任何建议?

【问题讨论】:

    标签: winforms wpf-controls elementhost


    【解决方案1】:

    我知道这个问题已经得到解答,而且这个问题已经过时了,但是在对问题进行了很长时间的故障排除之后,所提供的答案都没有对我有用。我终于找到了一个更简单的答案。

    如果您在初始构造函数中构建从 Element Host 扩展的类。您可以为主机容器设置加载事件。 Host Container 是 Element Hosts Child 显示在上面的面板。从那里,只需将 Host Containers 背景颜色设置为 Element Hosts Parents 背景颜色。

    这样

        using System.Windows;
        using System.Windows.Forms;
        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));
            }
        }
    

    【讨论】:

      【解决方案2】:

      您需要第一次显示具有空边界的控件以避免黑色闪烁

      if (!_control.Created && _control.BackColor != Color.Transparent)
      {
          _control.Bounds = Rectangle.Empty;
          _control.Show();
      }
      
      // set control bounds and show it
      Rectangle bounds = GetBounds(context, rect);
      if (_control.Bounds != bounds)
          _control.Bounds = bounds;
      if (!_control.Visible)
          _control.Show();
      

      【讨论】:

      • 您能详细说明一下吗?我很难弄清楚这件事的背景......
      【解决方案3】:

      隐藏元素 (Visibility = Hidden) 直到 WinForms 控件完全加载...

      【讨论】:

      • 是的,可以。不过,我不得不隐藏 ElementHost。如果我只是将 wpf 元素设置为 Visibility=Hidden,我仍然会得到闪光灯。但是感谢您为我指明了正确的方向。
      • WinForms 控件在哪一点被认为是“完全加载”?
      猜你喜欢
      • 1970-01-01
      • 2015-12-31
      • 1970-01-01
      • 2011-05-21
      • 1970-01-01
      • 2012-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多