We have a CRM VSTO Add-in for Microsoft Outlook, which displays a custom task pane which has a WinForm control inside a WPF form/control as one of its functionalities. But, since after Version 1812 and the latest update to Office 365 pro plus installations, in some of the client machines we have been facing the following error/exceptions and form doesn't show or load:
System.InvalidOperationException: Hosted HWND must be a child window of the specified parent. 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.FireOnVisualParentChanged(DependencyObject oldParent) at System.Windows.Media.Visual.AddVisualChild(Visual child) at System.Windows.Media.VisualCollection.ConnectChild(Int32 index, Visual value)
at System.Windows.Media.VisualCollection.Add(Visual visual) at System.Windows.Controls.UIElementCollection.AddInternal(UIElement element) at System.Windows.Controls.UIElementCollection.Add(UIElement element)
Basically, the code fails while embedding and displaying the WPF form. The exact code snippet causing the issue is as follows:
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); }
In the above code,
HostPanel: System.Windows.Controls.Grid
Children: UIElementCollection Panel
Also, the HostPanel or the grid has been used/placed in the XAML like below :
<Grid><!-- Form Panel --><Grid Name="HostPanel" Margin="0,50,0,0"></Grid>
And, in the XAML code partial class:
We are assigning the Winform control as a child of the WPF.
Then, we are trying to add the WPF form/UIElement in the UIElementCollection Panel of the grid control.
It is really difficult to narrow down to the exact root cause of the issue when everything was working as expected and just after pushing the updates to the Microsoft Office, the errors/exceptions are being seen.
Any help or pointers towards resolving the issue will be greatly appreciated.
Regards,
Pratik Prakash