Webbrowser control as part of addin

Feb 6, 2008 at 4:25 AM
I have referred to the WindowsControlsExtensibility solution to be able to load windows Forms controls within an Addin. Basically, what I have done is create a Windows Forms user control with a WebBrowser control in it. Then, I have a WPF user control, which contains a WindowsFormsHost control only in it, and into this WPF user control I load my Windows Forms user control, which has the WebBrowser. I use a WPF panel to load all these items into. Basically, I have some XML which I run through a transformer to get Html, and I load that as the content of the WebBrowser control.

Everything is loading up nicely when it comes to both the WPF addin and the Windows Forms stuff. However, when I close the application, I get an error as follows:
Message="Delegates cannot be marshaled from native code into a domain other than their home domain."
Source="WindowsBase"

Basically, I tried commenting and uncommenting bits and pieces of my code, and I found that this is the line which stops the problem if I uncomment it:
resultsPanel.Children.Add(enquiryResults);

resultsPanel is a WPF panel, enquiryResults is the WPF user control which loads the WinForms addin with the webBrowser control.

Again, all of the data is being rendered correctly, I only get this error message when closing the application.

Any ideas are appreciated.



Jan 5, 2009 at 8:56 PM
I'm having the exact same issue with the following code.
Note that it doesn't even create a System.Windows.Forms.WebBrowser, just the WindowsFormsHost is enough to cause the problem.

Any help out there?

XAML
---------------------------
<

 

UserControl x:Class="AOL.AddIn.Browser.BrowserTabPanel"
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml>
    <Grid Name="BrowserGrid">
        <Grid.RowDefinitions>
            <RowDefinition Name="BrowserControlsRow" Height="Auto"/>
            <RowDefinition Name="BrowserContentRow" Height="*"/>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Margin="0,2,0,2" Orientation="Horizontal"/>
        <!-- System.Windows.Forms.WebBrowser Grid.Row="1" control added in C# -->
    </Grid>
</UserControl>


C#
-------------------------
public
BrowserTabPanel()
{
    InitializeComponent();
    m_browserHost =
new System.Windows.Forms.Integration.WindowsFormsHost();
    Grid.SetRow( m_browserHost, 1 );
    BrowserGrid.Children.Add( m_browserHost );
}
 

Jan 5, 2009 at 10:13 PM
Here's an update.  I only receive the "Delegates cannot be marshalled from native code..." exception if I create 2 or more instances of the BrowserTabPanel user control.  A single instance does not generate the exception when the app shuts down.
Jan 6, 2009 at 4:03 PM
I found a workaround.

I added an event to my add-in contract called ReleaseResources()

In my MainWindow.Closed event handler, I call ReleaseResources() on each add-in.

In my add-in that has the WebBrowser objects, I do this for each BrowserTabPanel object:

 

public void ReleaseResources()
{
    BrowserGrid.Children.Clear();

    if ( m_browserTabContent != null )
    {
        m_browserTabContent.Dispose();
        m_browserTabContent =
null;
    }

    if
( m_browserHost != null )
    {
        m_browserHost.Dispose();
        m_browserHost =
null;
    }
}

 

Presto, no more exceptions during shutdown!