在WPF用户界面元素添加到Winform网格控制面板时,如何解决InvalidOperationException?

时间:2019-04-11 18:29:19

标签: wpf winforms wpf-controls vsto invalidoperationexception

我们有一个用于Microsoft Outlook的CRM加载项,它显示了一个自定义任务窗格,该窗格在WPF表单/控件内部具有一个WinForm控件,作为其功能之一。但是,自从版本1812和Office 365专业增强版的最新更新之后,在某些客户端计算机中,我们一直面临以下错误/异常,并且表单不显示或加载:

  

System.InvalidOperationException:托管的HWND必须是子窗口   指定父级的。在   System.Windows.Interop.HwndHost.BuildWindow(HandleRef hwndParent)
  在System.Windows.Interop.HwndHost.BuildOrReparentWindow()处   System.Windows.Interop.HwndHost.OnSourceChanged(对象发送者,   SourceChangedEventArgs e)位于   System.Windows.SourceChangedEventArgs.InvokeEventHandler(委托   genericHandler,对象genericTarget))   System.Windows.RoutedEventArgs.InvokeHandler(委托处理程序,对象   目标)   System.Windows.RoutedEventHandlerInfo.InvokeHandler(对象目标,   RoutedEventArgs routedEventArgs)位于   System.Windows.EventRoute.InvokeHandlersImpl(对象源,   RoutedEventArgs args,布尔值重新引发)   System.Windows.UIElement.RaiseEventImpl(DependencyObject发送者,   RoutedEventArgs args)位于   System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)位于   System.Windows.PresentationSource.UpdateSourceOfElement(DependencyObject   doTarget,DependencyObject doAncestor,DependencyObject doOldParent)
  在   System.Windows.PresentationSource.OnVisualAncestorChanged(DependencyObject   uie,AncestorChangedEventArgs e)位于   System.Windows.UIElement.OnVisualAncestorChanged(对象发送者,   AncestorChangedEventArgs e)在   System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject   e,AncestorChangedEventArgs args)位于   System.Windows.Media.Visual.FireOnVisualParentChanged(DependencyObject   System.Windows.Media.Visual.AddVisualChild(Visual   child)在System.Windows.Media.VisualCollection.ConnectChild(Int32   索引,视觉值)   System.Windows.Media.VisualCollection.Add(Visual visual)位于   System.Windows.Controls.UIElementCollection.AddInternal(UIElement   元素)在System.Windows.Controls.UIElementCollection.Add(UIElement   元素)

基本上,代码在嵌入和显示WPF表单时失败。导致该问题的确切代码段如下:

 public void DisplayForm(System.Windows.Forms.Control form)
        {
            // Adapt WinForm Form Control to WPF           
            System.Windows.Forms.Integration.WindowsFormsHost wfh = new System.Windows.Forms.Integration.WindowsFormsHost();
            wfh.Child = form;
            wfh.Margin = new Thickness(0);

            // Display WPF Form            
            DisplayForm(wfh);            
        }


        // Embed a WPF Control       
        public void DisplayForm(UIElement form)
        {
            HostPanel.Children.Clear(); 

    //***Below line throws an exception***
            HostPanel.Children.Add(form);
        }

在上面的代码中,

HostPanel System.Windows.Controls.Grid

孩子 UIElementCollection面板

此外,已在XAML中使用/放置了HostPanel或网格,如下所示:

<Grid>

<!-- Form Panel -->
<Grid Name="HostPanel" Margin="0,50,0,0">
</Grid>

并且,在XAML代码子类中:

  1. 我们将Winform控件分配为WPF的子级。

  2. 然后,我们尝试在网格控件的UIElementCollection面板中添加WPF表单/ UIElement。

当一切都按预期运行时,要找出问题的确切根本原因确实很困难,并且在将更新推送到Microsoft Office之后,就看到了错误/异常。

对于解决问题的任何帮助或指示,将不胜感激。

谢谢!

**

更新

**

此问题似乎是由于Microsoft推出了Office support for High DPI

通过在底部状态栏的“显示设置”中或“常规Outlook”选项下的“用户界面”选项中选择“优化兼容性”,可以解决Outlook中的问题。

Image for the setting/configuration

但是,有没有一种方法可以使上述代码了解DPI并呈现呈现的表单控件而没有任何问题?

我还从此MSDN Article中得知,由于作为Office窗口的父级的子代无法响应DPI缩放,因此我可能无法实现。但是,对此的任何进一步投入将不胜感激。

谢谢。

0 个答案:

没有答案