I use the WebBrowser-Control in my WPF-Application like
<WebBrowser x:Name="webBrowser" Margin="0,28,0,0" />
Now, when I navigate to a mht-page which contains links and the user click on one of this link, the new page is opened in the WebBrowser-Control. But it should be opened in a new Default-Browser-Window. The content in the WebBrowser-Control should not be changed. Is there a way to change this behavior?
You can open the new page in default browser using Proces.Start() on Navigating event and set e.Cancel = true;
so that the page in the control will not change.
Example:
@MainWindow.xaml.cs
using System.Diagnostics;
using System.Windows;
using System.Windows.Navigation;
namespace OpenDefaultBrowser
{
public partial class MainWindow : Window
{
private static bool willNavigate;
public MainWindow()
{
InitializeComponent();
}
private void webBrowser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
// first page needs to be loaded in webBrowser control
if (!willNavigate)
{
willNavigate = true;
return;
}
// cancel navigation to the clicked link in the webBrowser control
e.Cancel = true;
var startInfo = new ProcessStartInfo
{
FileName = e.Uri.ToString()
};
Process.Start(startInfo);
}
}
}
@MainWindow.xaml
<Window x:Class="OpenDefaultBrowser.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="464" Width="1046">
<Grid>
<WebBrowser Height="425" HorizontalAlignment="Left" Name="webBrowser1" VerticalAlignment="Top" Width="1024" Source="http://stackoverflow.com/" Navigating="webBrowser1_Navigating" />
</Grid>
</Window>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With