我在 WPF 应用程序中使用 WebBrowser-Control,例如
<WebBrowser x:Name="webBrowser" Margin="0,28,0,0" />
现在,当我导航到包含链接的 mht 页面并且用户单击其中一个链接时,新页面将在 WebBrowser-Control 中打开。但它应该在新的默认浏览器窗口中打开。 WebBrowser-Control 中的内容不应更改。
有办法改变这种行为吗?
您可以使用默认浏览器打开新页面进程.Start() http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx导航事件并设置e.Cancel = true;
这样控件中的页面就不会改变。
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)