how to host webbrowser in wpf?
-
Tuesday, April 24, 2007 2:01 PM
I am trying to use webbrowser with wpf. I tried:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace
ystem.Windows.Forms;assembly=System.Windows.Forms">
<Grid x:Name="myGrid">
<StackPanel VerticalAlignment="Center"><wf:WebBrowser x:Name="TestWebBrowser=" Url="http://www.yahoo.com"/>
... ...Besides, assuming it would work, how to communicate with it, i.e. catch the WebBrowser's events, calling its function?
Thanks for your help.
All Replies
-
Tuesday, April 24, 2007 3:13 PMModerator
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:59 PM
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-namespace
ystem.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 4:52 PMModerator
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 5:53 PM
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 7:06 PM
I made it work in VStudio, still get error with Blend though.
Thanks Drew.
-
Tuesday, April 24, 2007 7:25 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 9:30 PMModerator
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 -
Wednesday, April 25, 2007 1:57 PM
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 3:04 PMModerator
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

