Name of the article, and content taken from here
I was playing with WPF WebBrowser control, but it seems that WPF WebBrowser is either not finished yet, or I don't know how to use it. First problem is that it seems that I am unable to suppress script errors. Another problem was that I couldn't find easy way to check if WebBrowser finished loading.
Add to reference list System.Windows.Forms, and WindowsFormsIntegration, like on the pictures:
Your XAML should look like:
<Window x:Class="WebBrowser.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded"> <Grid Name="grid1"> </Grid> </Window>
Notice lines:
Loaded="Window_Loaded"
and
<Grid Name="grid1">
To get line Loaded="Window_Loaded", you can also click on the window, go to Properties, events, and double click on loaded:
Then your code behind should look like:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost(); System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser(); host.Child = browser; this.grid1.Children.Add(host); browser.ScriptErrorsSuppressed = true; browser.Navigate("http://www.milosev.com"); } }
Or, if you want to use browser in some another class, then it should look like:
public partial class MainWindow : Window { public static System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser(); public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost(); host.Child = browser; this.grid1.Children.Add(host); browser.ScriptErrorsSuppressed = true; } }
Notice that browser is declared a static:
public static System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
You can access it like:
MainWindow.browser.Navigate("http://www.milosev.com");
Example project you can download from here.