I'm having a major performance issue regarding the first load of a WPF control embedded in a Forms UserControl, it takes between 3 and 4 seconds just to add an ElementHost object at runtime.
I am using Visual Studio 2008 for a .net 3.5 app.
I have the following setup:
A Forms container with a single Panel:
using System.Windows.Forms;
using System.Windows.Forms.Integration;
namespace MyNamespace
{
public partial class TestContainer : UserControl
{
private ElementHost ctrlHost;
private TestWPFControl wpfAddressCtrl;
public TestContainer()
{
InitializeComponent();
ctrlHost = new ElementHost();
ctrlHost.Dock = DockStyle.Fill;
panel1.Controls.Add(ctrlHost); //slow!
wpfAddressCtrl = new TestWPFControl();
wpfAddressCtrl.InitializeComponent();
ctrlHost.Child = wpfAddressCtrl;
}
}
}
A standard VS2008 generated WPF control:
namespace MyNamespace
{
public partial class TestWPFControl : UserControl
{
public TestWPFControl()
{
InitializeComponent();
}
}
}
Accompanying XAML:
<UserControl x:Class="MyNamespace.TestWPFControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="382" Width="467">
<Grid>
<Button Height="23" Margin="170,46,222,0" Name="button1" VerticalAlignment="Top">Button</Button>
</Grid>
The WPF control (for testing purposes) only has 1 single Button, no logic or anything.
Once I Create an instance of the TestContainer, it takes roughly 3 to 4 seconds for it to show the WPF.
While debugging the above, the line
panel1.Controls.Add(ctrlHost);
is the slowing factor.
Profiling with Timer:
new ElementHost(): 23ms
ElementHost.DockStyle: 0ms
'C:\Windows\assembly\GAC_MSIL\WindowsFormsIntegration\3.0.0.0__31bf3856ad364e35\WindowsFormsIntegration.dll'
Panel.Controls.Add(ElementHost): 4130ms
The loading of WindowsFormsIntegration.dll seems to take ~4 seconds on every cold boot.
This is my first time trying to add a WPF control to a Forms element and I've no idea what is causing, let alone how to solve the huge first-load time of adding an ElementHost to a forms control.
For the record, the second time I create a new instance of the TestContainer is fast as expected.