C# 在COM加载项中使用WindowsFormsHost会在windows更新和office升级后引发ApplicationException
我一直在使用以下xaml在我的WPF对话框中托管windows窗体控件:C# 在COM加载项中使用WindowsFormsHost会在windows更新和office升级后引发ApplicationException,c#,wpf,winforms,office-interop,windowsformshost,C#,Wpf,Winforms,Office Interop,Windowsformshost,我一直在使用以下xaml在我的WPF对话框中托管windows窗体控件: <Grid x:Name="ConnectionGrid" DockPanel.Dock="Bottom" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Stretch" Margin="0,3,0,15" MinWidth="730"> <Grid.RenderTransform> <TransformGr
<Grid x:Name="ConnectionGrid" DockPanel.Dock="Bottom" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Stretch" Margin="0,3,0,15" MinWidth="730">
<Grid.RenderTransform>
<TransformGroup>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Grid.RenderTransform>
<WindowsFormsHost Name="WinFormsConnectionHost" Visibility="Visible"></WindowsFormsHost>
</Grid>
在Windows和Microsoft Office的最新更新之后,这将为我们的一些用户引发以下异常
Hosted HWND must be a child window of the specified parent. (InvalidOperationException)
AutoTag version: 16.5.0.14
Office: 16 (32-bit), WORD
Windows: Microsoft Windows NT 10.0.17763.0 (64-bit), GC memory: 26,954,252
Stack trace:
Exception: System.InvalidOperationException
Message: Hosted HWND must be a child window of the specified parent.
Stack: at System.Windows.Interop.HwndHost.BuildWindow(HandleRef hwndParent)
at System.Windows.Interop.HwndHost.BuildOrReparentWindow()
at System.Windows.Interop.HwndHost.OnSourceChanged(Object sender, SourceChangedEventArgs e)
at System.Windows.SourceChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.PresentationSource.UpdateSourceOfElement(DependencyObject doTarget, DependencyObject doAncestor, DependencyObject doOldParent)
at System.Windows.PresentationSource.OnVisualAncestorChanged(DependencyObject uie, AncestorChangedEventArgs e)
at System.Windows.UIElement.OnVisualAncestorChanged(Object sender, AncestorChangedEventArgs e)
at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
at System.Windows.Media.Visual.ProcessAncestorChangedNotificationRecursive(DependencyObject e, AncestorChangedEventArgs args)
at System.Windows.Media.Visual.FireOnVisualParentChanged(DependencyObject oldParent)
at System.Windows.Media.Visual.AddVisualChild(Visual child)
at System.Windows.FrameworkElement.set_TemplateChild(UIElement value)
at System.Windows.Controls.ContentPresenter.UseContentTemplate.BuildVisualTree(FrameworkElement container)
at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)
at System.Windows.FrameworkElement.ApplyTemplate()
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.DockPanel.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Border.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Control.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Border.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Window.MeasureOverrideHelper(Size constraint)
at System.Windows.Window.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
at System.Windows.Window.CreateSourceWindowDuringShow()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at AutoTag.net.windward.autotag.Framework.DataSourceOptions_Click(OfficeDocument doc)
at AutoTag.net.windward.autotag.menu.AutoTagMenu.DataSourceOptions(OfficeDocument doc)
我们的产品是office COM插件的一部分,我可以通过设置以下环境来复制它:
Windows 10 1809(内部版本17763.557)
Microsoft Office 2019 Professional Plus 1906(构建11727.20230)
我也尝试过像下面这样以编程方式添加WindowsFormsHost,但我收到了相同的异常apon,将其添加到网格中
ConnectionGrid.Children.Clear();
var host = new WindowsFormsHost();
host.Child = winformsControl;
host.HorizontalAlignment = HorizontalAlignment.Center;
host.VerticalAlignment = VerticalAlignment.Center;
StyleHostedWinFormsControl(winformsControl);
ConnectionGrid.Children.Add(host);
此问题似乎仅在使用最新的Office更新时出现。这可能会对您有所帮助?是否尝试为windows窗体控件设置父窗口句柄?很遗憾,设置父窗口句柄似乎没有任何效果。只有当加载项在Office 2019 1906中运行时,才会出现此错误。它在早期版本的Office中运行良好。我还尝试使用Dispatch.Invoke()将winformshost添加到我的对话框传递到UI,但运气不佳。也许这会对您有所帮助?是否尝试为windows窗体控件设置父窗口句柄?很遗憾,设置父窗口句柄似乎没有任何效果。只有当加载项在Office 2019 1906中运行时,才会出现此错误。它在早期版本的Office中运行良好。我还尝试使用Dispatch.Invoke()将winformshost添加到对话框中传递到UI,但没有成功。
ConnectionGrid.Children.Clear();
var host = new WindowsFormsHost();
host.Child = winformsControl;
host.HorizontalAlignment = HorizontalAlignment.Center;
host.VerticalAlignment = VerticalAlignment.Center;
StyleHostedWinFormsControl(winformsControl);
ConnectionGrid.Children.Add(host);