【发布时间】:2011-01-06 10:36:18
【问题描述】:
我在带有 ElementHost 的 WinForms 中使用 WPF。当表单加载时,ElementHost 即将加载的地方会出现一闪而过的黑色背景。这看起来有点糟糕。有关如何摆脱这种情况的任何建议?
【问题讨论】:
标签: winforms wpf-controls elementhost
我在带有 ElementHost 的 WinForms 中使用 WPF。当表单加载时,ElementHost 即将加载的地方会出现一闪而过的黑色背景。这看起来有点糟糕。有关如何摆脱这种情况的任何建议?
【问题讨论】:
标签: winforms wpf-controls elementhost
我知道这个问题已经得到解答,而且这个问题已经过时了,但是在对问题进行了很长时间的故障排除之后,所提供的答案都没有对我有用。我终于找到了一个更简单的答案。
如果您在初始构造函数中构建从 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));
}
}
【讨论】:
您需要第一次显示具有空边界的控件以避免黑色闪烁
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();
【讨论】:
隐藏元素 (Visibility = Hidden) 直到 WinForms 控件完全加载...
【讨论】: