Reputation: 699
I'm building a WPF application which contains a WebBrowser. I would like to use the Document.GetElementByID
method with the webbrowser, and my understanding is that the easiest way to do this in WPF is to use the winforms webbrowser and WindowsFormsIntegration (If there's a better way please let me know)
I'm having a problem navigating to the URL. Running the program in debug mode does not throw any errors, and stepping over the navigate code still leaves my webbrowser with the following properties:
wb1.ReadyState = Uninitialized
wb1.Url = null
Am I missing something for navigating to the url? Can I use the Document.GetElementById
methods in a WPF webbrowser?
xaml:
<Window x:Class="my program's main window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
...
<WindowsFormsHost Name="wfh">
<WindowsFormsHost.Child>
<wf:WebBrowser/>
</WindowsFormsHost.Child>
</WindowsFormsHost>
Code:
var wb1 = wfh.Child as System.Windows.Forms.WebBrowser;
wb1.Navigate("my url here");
while (wb1.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
// this loop never ends because neither readystate nor wb1.Url ever change
}
Upvotes: 0
Views: 2720
Reputation: 128146
Add a reference to Microsoft.mshtml
(from Assembies > Extensions) to your project. Then use the WPF WebBrowser control and cast its Document
property to HTMLDocument
:
<WebBrowser x:Name="webBrowser" Navigated="WebBrowserNavigated" />
Code behind:
using mshtml;
...
webBrowser.Navigate("...");
...
private void WebBrowserNavigated(object sender, NavigationEventArgs e)
{
var document = webBrowser.Document as HTMLDocument;
...
}
Upvotes: 2