我想创建“浏览器中”导航,每当单击链接时将弹出一个新窗口(IE)时,我都希望它取消并在浏览器中导航。我进行了搜索并遵循了一些指南,但是我不能阻止窗口在其浏览器上进行导航。下面是我在做什么:-
XAML:-
<WebBrowser Name="HostWeb" Navigating="HostWeb_Navigating" LoadCompleted="HostWeb_LoadCompleted" DockPanel.Dock="Top"/>
CS:-
using mshtml;
public partial class MainWindow : Window
{
Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
public MainWindow()
{
InitializeComponent();
HostWeb.Navigate("https://www.booking.com");
}
private void HostWeb_Navigating(object sender, NavigatingCancelEventArgs e)
{
Task.Factory.StartNew(() =>
{
Dispatcher.BeginInvoke(new Action(() =>
{
ProgressGrid.Visibility = Visibility.Visible;
if (e.Uri.OriginalString == "https://www.booking.com/content/terms.html")
{
// Cancel navigation of this URL. This will be load into Generic List to find with LINQ
e.Cancel = true;
}
else
{
NavigatedURI = e.Uri.OriginalString;
}
}), DispatcherPriority.Background);
});
}
private void HostWeb_LoadCompleted(object sender, NavigationEventArgs e)
{
Task.Factory.StartNew(() =>
{
ProgressGrid.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { ProgressGrid.Visibility = Visibility.Hidden; }));
IServiceProvider serviceProvider = (IServiceProvider)HostWeb.Document;
Guid serviceGuid = SID_SWebBrowserApp;
Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;
SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);
SHDocVw.DWebBrowserEvents_Event wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;
wbEvents.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);
});
}
void OnWebBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
Processed = true;
HostWeb.Navigate(URL);
}
private void BrowseBack_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = ((HostWeb != null) && (HostWeb.CanGoBack));
}
private void BrowseBack_Executed(object sender, ExecutedRoutedEventArgs e)
{
HostWeb.GoBack();
}
private void BrowseForward_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = ((HostWeb != null) && (HostWeb.CanGoForward));
}
private void BrowseForward_Executed(object sender, ExecutedRoutedEventArgs e)
{
HostWeb.GoForward();
}
private void GoToHome_Click(object sender, RoutedEventArgs e)
{
HostWeb.Navigate("https://www.booking.com");
}
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
internal interface IServiceProvider
{
[return: MarshalAs(UnmanagedType.IUnknown)]
object QueryService(ref Guid guidService, ref Guid riid);
}
第二种方法是我无法完成它,因为HostWeb.ActiveXInstance;在SHDocVw上不存在:-
using SHDocVw;
public MainWindow()
{
InitializeComponent();
WebBrowser_V1 axBrowser = (WebBrowser_V1)HostWeb.ActiveXInstance;
axBrowser.NewWindow += AxBrowser_NewWindow;
HostWeb.Navigate("https://www.booking.com");
}
private void HostWeb_LoadCompleted(object sender, NavigationEventArgs e)
{
Task.Factory.StartNew(() =>
{
ProgressGrid.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { ProgressGrid.Visibility = Visibility.Hidden; }));
HTMLDocument htmlDoc = (HTMLDocument)HostWeb.Document;
IHTMLElementCollection htmlElementCollection = htmlDoc.getElementsByTagName("A");
//Set All Anchore Target As _self For Stop Opening New Window......
foreach (IHTMLElement curElement in htmlElementCollection)
{
string targetAtt = curElement.getAttribute("target");
if (targetAtt == "_blank" || targetAtt == "_top" || targetAtt == "_parent" || targetAtt == "Array")
{
curElement.removeAttribute("target");
curElement.setAttribute("target", "_self");
}
}
});
}
void AxBrowser_NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
{
// cancel the PopUp event
Processed = true;
// send the popup URL to the WebBrowser control
HostWeb.Navigate(URL);
}
给出Booking.com网络的示例,如何防止在弹出之前弹出新标签页/新窗口 并通过当前浏览器进行导航(在当前页面中打开新标签页/窗口) ?
附加:
给定booking.com页面具有4个主菜单[住宿,航班,租车,机场出租车],则当两者都不单击时,它将弹出并且不会发生HostWeb_Navigating
事件。
答案 0 :(得分:1)
您添加对COM-> Microsoft Internet Controls的引用并处理LoadCompleted
事件:
static readonly Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
internal interface IServiceProvider
{
[return: MarshalAs(UnmanagedType.IUnknown)]
object QueryService(ref Guid guidService, ref Guid riid);
}
private void WebBrowser_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
IServiceProvider serviceProvider = (IServiceProvider)webBrowser.Document;
Guid serviceGuid = SID_SWebBrowserApp;
Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;
SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);
SHDocVw.DWebBrowserEvents_Event wbEvents = (SHDocVw.DWebBrowserEvents_Event)myWebBrowser2;
wbEvents.NewWindow += new SHDocVw.DWebBrowserEvents_NewWindowEventHandler(OnWebBrowserNewWindow);
}
private void OnWebBrowserNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers,
ref bool Processed)
{
Processed = true;
webBrowser.Navigate(URL);
}
XAML:
<WebBrowser x:Name="webBrowser" Source="http://booking.com" LoadCompleted="WebBrowser_LoadCompleted" />