Compartilhar via


Troubleshooting Hybrid Applications

This topic lists some common problems that can occur when authoring hybrid applications which use both WPF and Windows Forms technologies.

Overlapping Controls

Controles não podem se sobrepor sistema autônomo você esperaria. Windows Forms usa o HWND separado para cada controle. WPF usa um HWND para todo o conteúdo em uma página. This implementation difference causes unexpected overlapping behaviors.

A Windows Forms control hosted in WPF always appears on top of the WPF content.

WPF content hosted in an ElementHost control appears at the z-order of the ElementHost control. It is possible to overlap ElementHost controls, but the hosted WPF content does not combine or interact.

Child Property

The WindowsFormsHost and 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 System.Windows.Forms.Panel control, and then assign the panel to a WindowsFormsHost control's Child property. However, you cannot add the button and check box controls separately to the same WindowsFormsHost control.

Dimensionamento

WPF and Windows Forms have different scaling models. Some WPF scaling transformations are meaningful to Windows Forms controls, but others are not. Por exemplo, dimensionando um Windows Forms controle como 0 funcionará, mas se você tentar dimensionar o mesmo controle novamente para um valor diferente de zero, o tamanho do controle permanece 0. Para obter mais informações, consulte Considerações sobre layout para o elemento WindowsFormsHost.

Adapter

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

Nesting

Nesting a WindowsFormsHost element inside an ElementHost control is not supported. Nesting an ElementHost control inside a WindowsFormsHost element is also not supported.

Foco

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

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

Property Mapping

Some property mappings require extensive interpretation to bridge dissimilar implementations between the WPF 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. Para obter mais informações, consulte Formulários do Windows e mapeamento de propriedades do WPF.

When the WindowsFormsHost.Child or ElementHost.Child 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 WindowsFormsHost and 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 ElementHost and WindowsFormsHost classes.

Host Class

Content Properties

ElementHost

Height

Width

Margin

VerticalAlignment

HorizontalAlignment

WindowsFormsHost

Margin

Dock

AutoSize

Location

MaximumSize

Do not set these properties directly on the hosted content. Para obter mais informações, consulte Considerações sobre layout para o elemento WindowsFormsHost.

Navigation applications may not maintain user state. The 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 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 EnableWindowsFormsInterop method is called by the WindowsFormsHost constructor. This method adds a message filter to the WPF message loop. This filter calls the Control.PreProcessMessage method if a System.Windows.Forms.Control was the target of the message and translates/dispatches the message.

If you show a Window in a Windows Forms message loop with Application.Run, you cannot type anything unless you call the EnableModelessKeyboardInterop method. The EnableModelessKeyboardInterop method takes a Window and adds an System.Windows.Forms.IMessageFilter which re-routes key-related messages to the WPF message loop. Para obter mais informações, consulte Arquitetura de entrada para interoperabilidade entre Windows Forms e WPF.

Opacity and Layering

The HwndHost class does not support layering. This means that setting the Opacity property on the WindowsFormsHost element has no effect, and no blending will occur with other WPF windows which have AllowsTransparency set to true.

Dispose

Not disposing classes properly can leak resources. Nos aplicativos híbridos, certifique-se de que o WindowsFormsHost e ElementHost classes são descartados ou pode vazar recursos. Windows Forms descarta ElementHost Controla quando o não-restrita Form pai é fechada. WPF descarta WindowsFormsHost elementos quando seu aplicativo é desligado. It is possible to show a WindowsFormsHost element in a 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 Application.EnableVisualStyles method is called in the template for a Windows Forms application. Embora esse método não é chamado por padrão, se você usar Visual Studio Para criar um projeto, você obterá Microsoft Windows XP estilos visuais para controles, se a versão 6.0 do Comctl32.dll estiver disponível. Você deve chamar o EnableVisualStyles método antes que sejam criadas alças no segmento. Para obter mais informações, consulte Como: Ativar estilos visuais em um aplicativo híbrido.

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 WindowsFormsHost element derives from the HwndHost class, and the child control’s handle is created inside the BuildWindowCore method. The HwndHost class does not allow messages to be processed in the BuildWindowCore method, but displaying a dialog box causes messages to be sent. To enable this licensing scenario, call the Control.CreateControl method on the control before assigning it as the WindowsFormsHost element's child.

WPF Designer

You can design your WPF content by using the Windows Presentation Foundation (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 BackColorTransparent property might not work as expected at design time.

If a WPF control is not on a visible parent, the WPF runtime ignores the BackColorTransparent value. The reason that BackColorTransparent might be ignored is because ElementHost object is created in a separate AppDomain. However, when you run the application, BackColorTransparent 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 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. Para obter mais informações, consulte Erros de tempo de design no Windows Forms Designer.

ElementHost and IME

WPF controls hosted in an ElementHost currently do not support the ImeMode property. Changes to ImeMode will be ignored by the hosted controls.

Consulte também

Tarefas

Como: Ativar estilos visuais em um aplicativo híbrido

Conceitos

Arquitetura de entrada para interoperabilidade entre Windows Forms e WPF

Considerações sobre layout para o elemento WindowsFormsHost

Formulários do Windows e mapeamento de propriedades do WPF

Erros de tempo de design no Windows Forms Designer

Referência

ElementHost

WindowsFormsHost

Outros recursos

Interoperabilidade no designer do WPF

Migração e Interoperabilidade