How to host a windows form application inside XBAP
I am writing a series of posts about WPF Browser Application, XBAP and configuration tips. I’m going to host this application in IIS 5.1 and I developed them in .Net 3.5. The reason I have chosen IIS5.1 and .Net 3.5 is because of challenges I had in one of my recent projects. Configuring this type of projects is different in other versions of IIS and .Net frameworks and they are not is subject of this post series.
- How to create a simple Browser Enables WPF application
- How to host a windows form application inside XBAP
- How to sign the XBAP with your own certificate

<StackPanel x:Name="stackPanel"> </StackPanel>
Actually this is a place holder for WindowsFormsHost that we are going to place in main form. For next step we need add System.Windows.Froms reference to WBP project. Then we have to change page1.xaml code as follow.
public partial class Page1 : Page
{
private readonly Form1 mainForm = new Form1();
WindowsFormsHost windowsFormsHost;
public Page1()
{
InitializeComponent();
AddWindowsForm();
}
private void AddWindowsForm()
{
windowsFormsHost = new WindowsFormsHost();
stackPanel.Children.Add(windowsFormsHost);
// If you don't write this line you'll get "The child control cannot be a top-level form" exception
mainForm.TopLevel = false;
windowsFormsHost.Child = mainForm;
}
}
We created a WindowsFormHost and added this control into stackPanel Child list and set the child property of windowsFormsMost to mainForm which is already instantiated of Form1.
One of important thing is setting mainForm.TopLevel to false. Because if you don’t do that you will get an exception and if you dive into innerexeptions you will find out that main reason is System.ArgumentException: The child control cannot be a top-level form.
If you run this application you’ll see following browser window which hosts Form1.

The point is when you run this application from visual studio it runs in My Computer Zone so there is no problem for security. According Microsoft document “WPF Partial Trust Security” section “Partial Trust Programming” when you run WPF application which requires full trust and current zone is “My Computer” behavior is “Automatic full trust” and for getting full trust no action is required.
But if you publish this project and try to browse this application you will get Trust Not Granted error. Because application will request for full trust and it fails with “Trust Not Granted”. In order to get full trust is signing XBAP with certificate.

In the next post we will see how to sign XBAP with your own certificate and make it work.
The source code of this application is also available you can download it here


[...] How to host a windows form application inside XBAP [...]
How to create a simple Browser Enabled WPF application « Ahmadreza's Notes
May 17, 2011 at 9:30 am
[...] How to host a windows form application inside XBAP [...]
How to sign the XBAP with your own certificate « Ahmadreza's Notes
May 20, 2011 at 9:36 am