您可以在導航事件上使用Proces.Start()在默認瀏覽器中打開新頁面,並設置e.Cancel = true;
以便控件中的頁面不會更改。
例子:
@ 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>
一個更安全的檢查可以是e.Uri == NULL。非點擊加載就是這種情況。 – t9mike