Mike Henderlight recently posted a nice summary of a bug in WinFX current (September CTP) bits: ActiveX Controls Not Working via XAML.
An attempt to instantiate an ActiveX control with a managed wrapper (Axhost) from XAML fails with an interesting exception. Something to do with a child HWND not being attached to the appropriate parent. Our crack dev, Scott Berry, thinks we're delay-creating a window when we shouldn't.
In any case, the workaround is to instantiate the ActiveX control in the code-behind file. Since I just finished writing a topic for the docs that describes how to do this, I thought it would be convenient to grab the baton from Mike and show you how to do the workaround. I've adapted my doc for the blog. Hope it helps.
In order to complete this walkthrough, you will need:
To Create the Project
Create a Windows Presentation Foundation application project named "HostingAxInWpf".
In Solution Explorer, add a reference to the Microsoft Windows Media Player assembly, which is named wmp.dll.
Open the Toolbox. Right-click in the Toolbox and select Choose Items… from the shortcut menu.
Click the COM Components tab and select the Windows Media Player control. Click OK to accept the selection. (The Microsoft Windows Media Player control is added to the Toolbox.)
In Solution Explorer, right-click the UserControl1 file. Select Rename from the shortcut menu and change the name to "WmpAxControl.cs." Click Yes if you are asked to rename all references.
To Create the ActiveX Control
Microsoft Visual Studio automatically generates an AxHost wrapper class for an ActiveX control when it is placed on a design surface. The following procedure creates a managed assembly named AxInterop.WMPLib.dll.
To Host the ActiveX Control on a Windows Presentation Foundation Page
<
>
</
5. Open Window1.xaml.cs and uncomment the definition of the WindowLoaded method. 6. Insert the following code to handle the Loaded event. This code creates an instance of the WindowsFormsHost control and adds an instance of the AxWindowsMediaPlayer control as its child. private void WindowLoaded(object sender, RoutedEventArgs e) { // Create the interop host control.
5. Open Window1.xaml.cs and uncomment the definition of the WindowLoaded method.
6. Insert the following code to handle the Loaded event. This code creates an instance of the WindowsFormsHost control and adds an instance of the AxWindowsMediaPlayer control as its child.
private void WindowLoaded(object sender, RoutedEventArgs e)
{
// Create the interop host control.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create the ActiveX control.
AxWMPLib.AxWindowsMediaPlayer axWmp = new AxWMPLib.AxWindowsMediaPlayer();
// Add the ActiveX control to the host control's
// collection of child controls.
host.Children.Add(axWmp);
// Add the interop host control to the Grid
// control's collection of child controls.
this.grid1.Children.Add(host);
// Play a .wav file with the ActiveX control.
axWmp.URL = @"C:\WINDOWS\Media\Windows XP Startup.wav";
}
7. Press F5 to build and run the application.
Hopefully, that was relatively quick and painless.