none
how to host webbrowser in wpf?

Answers

  • First, are you aware of WPF's intrinsic Frame control? It might provide you with all you need without having to go with Windows Forms interop.

     

    That said, the problem is you have to place your Windows Forms WebBrowser into a WindowsFormsHost. Handling events is as easy as attaching to the WebBrowser control's events just like you would in WF. If you wanted to use XAML for hooking up events you can do that as well, though typically I stay away from that.

     

    HTH,
    Drew

    Tuesday, April 24, 2007 3:13 PM
    Moderator

All replies

  • First, are you aware of WPF's intrinsic Frame control? It might provide you with all you need without having to go with Windows Forms interop.

     

    That said, the problem is you have to place your Windows Forms WebBrowser into a WindowsFormsHost. Handling events is as easy as attaching to the WebBrowser control's events just like you would in WF. If you wanted to use XAML for hooking up events you can do that as well, though typically I stay away from that.

     

    HTH,
    Drew

    Tuesday, April 24, 2007 3:13 PM
    Moderator
  • Thanks Drew.

    I added the WindowsFormsHost (copied code from MSDN) and still does not show anything.

     

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespaceTongue Tiedystem.Windows.Forms;assembly=System.Windows.Forms" 
        Title="HostingWfInWpf"
        >

        <Grid>

            <WindowsFormsHost>
                 <wf:WebBrowser x:Name="TestWebBrowser=" Url="http://www.yahoo.com"/>
            </WindowsFormsHost>

        </Grid>   
    </Window>

     

    Besides, what I'm trying is to use webbrowser as a dhtml editor. Is there better approach? Frame is good for displaying the html page, can it be used for editing?

     

    Tuesday, April 24, 2007 3:59 PM
  • First, no, Frame doesn't support editing, so go ahead and stick with the WinForms WebBrowser.

     

    Second you have an equals sign in the name of the web browser which isn't allowed. Other than that I see no problems and in fact, I plopped your XAML right into Blend and it worked.

     

    HTH,
    Drew

     

    Tuesday, April 24, 2007 4:52 PM
    Moderator
  •  

    I tried it in Blend too, withsame code with the name property removed.  and got:

    "Build project: test1, Targets: Build
    __________________________________________________
    Project "C:\Users\linma9\Documents\Expression\Expression Blend Projects\test1\test1.csproj" (Build target(s)):

    Window1.xaml(10,6): error MC3074: The tag 'WindowsFormsHost' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. Line 10 Position 6.
    Done building project "test1.csproj" -- FAILED.
    Build failed.
    "

    Actually as soon as  the code is put in the editor it shows two errors. One says WindowsFormsHost is not in the namespace, and the other says WebBrowser is not in the namespace.

    I am running Vista.

    There must be something missing.

    Tuesday, April 24, 2007 5:53 PM
  • I made it work in VStudio, still get error with Blend though.

    Thanks Drew.

    Tuesday, April 24, 2007 7:06 PM
  • running into other problem, if I do it n xaml markup, it shows the page.

    <WindowsFormsHost>

    <swf:WebBrowser x:Name="TestWebBrowser" Url="www.hotmail.com"/>

    </WindowsFormsHost>

    but if do it in code (I did in load event), it does not show anything.

    private void Window_Loaded_WebBrowser(object sender, RoutedEventArgs e)

    {

    WindowsFormsHost host = new WindowsFormsHost();

    WebBrowser webb = new WebBrowser();

    host.Child = webb;

    webb.Url = new Uri(http://www.yahoo.com);

    }

     

    Any idea?

    Thanks for your help.

    Tuesday, April 24, 2007 7:25 PM
  • I don't see you adding "host" to the logical tree in that method. You need to add it to your Grid's (or whatever container you're using) Children collection.

     

    HTH,
    Drew

    Tuesday, April 24, 2007 9:30 PM
    Moderator
  • Thanks Drew.

    My bad not setting the host to a parent element in the snippet.

    It worked when set the Uri like www.yahoo.com in markup <wf:WebBrowser x:Name="TestWebBrowser" Url="www.yahoo.com"/>, but failed when doing so in code:

    TestWebBrowser.Url = new Uri("www.yahoo.com");

     

     

     

     

    Wednesday, April 25, 2007 1:57 PM
  • I think it's because you need to explicitly set the protocol to "http://" when setting the Uri. In XAML I believe the converter automatically assumes http when no protocol is present.

     

    HTH,
    Drew

    Wednesday, April 25, 2007 3:04 PM
    Moderator