Files
docs-desktop/dotnet-desktop-guide/framework/wpf/advanced/troubleshooting-hybrid-applications.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

16 KiB
Raw Blame History

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
Troubleshooting Hybrid Applications 03/30/2017
overlapping controls [WPF]
Windows Forms [WPF], interoperability with
Windows Forms [WPF], WPF interoperation
interoperability [WPF], Windows Forms
hybrid applications [WPF interoperability]
message loops [WPF]
f440c23f-fa5d-4d5a-852f-ba61150e6405

Troubleshooting Hybrid Applications

This topic lists some common problems that can occur when authoring hybrid applications, which use both [!INCLUDETLA2#tla_winclient] and Windows Forms technologies.

Overlapping Controls

Controls may not overlap as you would expect. Windows Forms uses a separate HWND for each control. [!INCLUDETLA2#tla_winclient] uses one HWND for all content on a page. This implementation difference causes unexpected overlapping behaviors.

A Windows Forms control hosted in [!INCLUDETLA2#tla_winclient] always appears on top of the [!INCLUDETLA2#tla_winclient] content.

[!INCLUDETLA2#tla_winclient] content hosted in an xref:System.Windows.Forms.Integration.ElementHost control appears at the z-order of the xref:System.Windows.Forms.Integration.ElementHost control. It is possible to overlap xref:System.Windows.Forms.Integration.ElementHost controls, but the hosted [!INCLUDETLA2#tla_winclient] content does not combine or interact.

Child Property

The xref:System.Windows.Forms.Integration.WindowsFormsHost and xref:System.Windows.Forms.Integration.ElementHost classes can host only a single child control or element. To host more than one control or element, you must use a container as the child content. For example, you could add Windows Forms button and check box controls to a xref:System.Windows.Forms.Panel?displayProperty=nameWithType control, and then assign the panel to a xref:System.Windows.Forms.Integration.WindowsFormsHost control's xref:System.Windows.Forms.Integration.WindowsFormsHost.Child%2A property. However, you cannot add the button and check box controls separately to the same xref:System.Windows.Forms.Integration.WindowsFormsHost control.

Scaling

[!INCLUDETLA2#tla_winclient] and Windows Forms have different scaling models. Some [!INCLUDETLA2#tla_winclient] scaling transformations are meaningful to Windows Forms controls, but others are not. For example, scaling a Windows Forms control to 0 will work, but if you try to scale the same control back to a non-zero value, the control's size remains 0. For more information, see Layout Considerations for the WindowsFormsHost Element.

Adapter

There may be confusion when working the xref:System.Windows.Forms.Integration.WindowsFormsHost and xref:System.Windows.Forms.Integration.ElementHost classes, because they include a hidden container. Both the xref:System.Windows.Forms.Integration.WindowsFormsHost and xref:System.Windows.Forms.Integration.ElementHost classes have a hidden container, called an adapter, which they use to host content. For the xref:System.Windows.Forms.Integration.WindowsFormsHost element, the adapter derives from the xref:System.Windows.Forms.ContainerControl?displayProperty=nameWithType class. For the xref:System.Windows.Forms.Integration.ElementHost control, the adapter derives from the xref:System.Windows.Controls.DockPanel element. When you see references to the adapter in other interoperation topics, this container is what is being discussed.

Nesting

Nesting a xref:System.Windows.Forms.Integration.WindowsFormsHost element inside an xref:System.Windows.Forms.Integration.ElementHost control is not supported. Nesting an xref:System.Windows.Forms.Integration.ElementHost control inside a xref:System.Windows.Forms.Integration.WindowsFormsHost element is also not supported.

Focus

Focus works differently in [!INCLUDETLA2#tla_winclient] and Windows Forms, which means that focus issues may occur in a hybrid application. For example, if you have focus inside a xref:System.Windows.Forms.Integration.WindowsFormsHost element, and you either minimize and restore the page or show a modal dialog box, focus inside the xref:System.Windows.Forms.Integration.WindowsFormsHost element may be lost. The xref:System.Windows.Forms.Integration.WindowsFormsHost element still has focus, but the control inside it may not.

Data validation is also affected by focus. Validation works in a xref:System.Windows.Forms.Integration.WindowsFormsHost element, but it does not work as you tab out of the xref:System.Windows.Forms.Integration.WindowsFormsHost element, or between two different xref:System.Windows.Forms.Integration.WindowsFormsHost elements.

Property Mapping

Some property mappings require extensive interpretation to bridge dissimilar implementations between the [!INCLUDETLA2#tla_winclient] and Windows Forms technologies. Property mappings enable your code to react to changes in fonts, colors, and other properties. In general, property mappings work by listening for either PropertyChanged events or OnPropertyChanged calls, and setting appropriate properties on either the child control or its adapter. For more information, see Windows Forms and WPF Property Mapping.

When the xref:System.Windows.Forms.Integration.WindowsFormsHost.Child%2A?displayProperty=nameWithType or xref:System.Windows.Forms.Integration.ElementHost.Child%2A?displayProperty=nameWithType property is assigned, several layout-related properties on the hosted content are set automatically. Changing these content properties can cause unexpected layout behaviors.

Your hosted content is docked to fill the xref:System.Windows.Forms.Integration.WindowsFormsHost and xref:System.Windows.Forms.Integration.ElementHost parent. To enable this fill behavior, several properties are set when you set the child property. The following table lists which content properties are set by the xref:System.Windows.Forms.Integration.ElementHost and xref:System.Windows.Forms.Integration.WindowsFormsHost classes.

Host Class Content Properties
xref:System.Windows.Forms.Integration.ElementHost xref:System.Windows.FrameworkElement.Height%2A

xref:System.Windows.FrameworkElement.Width%2A

xref:System.Windows.FrameworkElement.Margin%2A

xref:System.Windows.FrameworkElement.VerticalAlignment%2A

xref:System.Windows.FrameworkElement.HorizontalAlignment%2A
xref:System.Windows.Forms.Integration.WindowsFormsHost xref:System.Windows.Forms.Control.Margin%2A

xref:System.Windows.Forms.Control.Dock%2A

xref:System.Windows.Forms.Control.AutoSize%2A

xref:System.Windows.Forms.Control.Location%2A

xref:System.Windows.Forms.Control.MaximumSize%2A

Do not set these properties directly on the hosted content. For more information, see Layout Considerations for the WindowsFormsHost Element.

Navigation Applications

Navigation applications may not maintain user state. The xref:System.Windows.Forms.Integration.WindowsFormsHost element recreates its controls when it is used in a navigation application. Recreating child controls occurs when the user navigates away from the page hosting the xref:System.Windows.Forms.Integration.WindowsFormsHost element and then returns to it. Any content that has been typed in by the user will be lost.

Message Loop Interoperation

When working with Windows Forms message loops, messages may not be processed as expected. The xref:System.Windows.Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop%2A method is called by the xref:System.Windows.Forms.Integration.WindowsFormsHost constructor. This method adds a message filter to the [!INCLUDETLA2#tla_winclient] message loop. This filter calls the xref:System.Windows.Forms.Control.PreProcessMessage%2A?displayProperty=nameWithType method if a xref:System.Windows.Forms.Control?displayProperty=nameWithType was the target of the message and translates/dispatches the message.

If you show a xref:System.Windows.Window in a Windows Forms message loop with xref:System.Windows.Forms.Application.Run%2A?displayProperty=nameWithType, you cannot type anything unless you call the xref:System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop%2A method. The xref:System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop%2A method takes a xref:System.Windows.Window and adds a xref:System.Windows.Forms.IMessageFilter?displayProperty=nameWithType, which reroutes key-related messages to the [!INCLUDETLA2#tla_winclient] message loop. For more information, see Windows Forms and WPF Interoperability Input Architecture.

Opacity and Layering

The xref:System.Windows.Interop.HwndHost class does not support layering. This means that setting the xref:System.Windows.UIElement.Opacity%2A property on the xref:System.Windows.Forms.Integration.WindowsFormsHost element has no effect, and no blending will occur with other [!INCLUDETLA2#tla_winclient] windows which have xref:System.Windows.Window.AllowsTransparency%2A set to true.

Dispose

Not disposing classes properly can leak resources. In your hybrid applications, make sure that the xref:System.Windows.Forms.Integration.WindowsFormsHost and xref:System.Windows.Forms.Integration.ElementHost classes are disposed, or you could leak resources. Windows Forms disposes xref:System.Windows.Forms.Integration.ElementHost controls when its non-modal xref:System.Windows.Forms.Form parent closes. [!INCLUDETLA2#tla_winclient] disposes xref:System.Windows.Forms.Integration.WindowsFormsHost elements when your application shuts down. It is possible to show a xref:System.Windows.Forms.Integration.WindowsFormsHost element in a xref:System.Windows.Window in a Windows Forms message loop. In this case, your code may not receive notification that your application is shutting down.

Enabling Visual Styles

Microsoft Windows XP visual styles on a Windows Forms control may not be enabled. The xref:System.Windows.Forms.Application.EnableVisualStyles%2A?displayProperty=nameWithType method is called in the template for a Windows Forms application. Although this method is not called by default, if you use Visual Studio to create a project, you will get Microsoft Windows XP visual styles for controls, if version 6.0 of Comctl32.dll is available. You must call the xref:System.Windows.Forms.Application.EnableVisualStyles%2A method before handles are created on the thread. For more information, see How to: Enable Visual Styles in a Hybrid Application.

Licensed Controls

Licensed Windows Forms controls that display licensing information in a message box to the user might cause unexpected behavior for a hybrid application. Some licensed controls show a dialog box in response to handle creation. For example, a licensed control might inform the user that a license is required, or that the user has three remaining trial uses of the control.

The xref:System.Windows.Forms.Integration.WindowsFormsHost element derives from the xref:System.Windows.Interop.HwndHost class, and the child controls handle is created inside the xref:System.Windows.Forms.Integration.WindowsFormsHost.BuildWindowCore%2A method. The xref:System.Windows.Interop.HwndHost class does not allow messages to be processed in the xref:System.Windows.Forms.Integration.WindowsFormsHost.BuildWindowCore%2A method, but displaying a dialog box causes messages to be sent. To enable this licensing scenario, call the xref:System.Windows.Forms.Control.CreateControl%2A?displayProperty=nameWithType method on the control before assigning it as the xref:System.Windows.Forms.Integration.WindowsFormsHost element's child.

WPF Designer

You can design your WPF content by using the WPF Designer for Visual Studio. The following sections list some common problems that can occur when authoring hybrid applications with the WPF Designer.

BackColorTransparent is ignored at design time

The xref:System.Windows.Forms.Integration.ElementHost.BackColorTransparent%2A property might not work as expected at design time.

If a WPF control is not on a visible parent, the WPF runtime ignores the xref:System.Windows.Forms.Integration.ElementHost.BackColorTransparent%2A value. The reason that xref:System.Windows.Forms.Integration.ElementHost.BackColorTransparent%2A might be ignored is because xref:System.Windows.Forms.Integration.ElementHost object is created in a separate xref:System.AppDomain. However, when you run the application, xref:System.Windows.Forms.Integration.ElementHost.BackColorTransparent%2A does work as expected.

Design-time Error List appears when the obj folder is deleted

If the obj folder is deleted, the Design-time Error List appears.

When you design using xref:System.Windows.Forms.Integration.ElementHost, the Windows Forms Designer uses generated files in the Debug or Release folder within your project's obj folder. If you delete these files, the Design-time Error List appears. To fix this problem, rebuild your project. For more information, see Design-Time Errors in the Windows Forms Designer.

ElementHost and IME

WPF controls hosted in an xref:System.Windows.Forms.Integration.ElementHost currently do not support the xref:System.Windows.Forms.Control.ImeMode%2A property. Changes to xref:System.Windows.Forms.Control.ImeMode%2A will be ignored by the hosted controls.

See also