只要搞懂Winform的  WPF稍微改一改就可以了

C# WPF Webbrowser 强制所有网页链接在同一页面打开-LMLPHP

主类:负责跳转的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using static System.Windows.Forms.AxHost; namespace Test
{ public class WebBrowserUrl2
{
public String Url { get; } public String Frame { get; } public WebBrowserUrl2(String url, String frame)
{
this.Url = url;
this.Frame = frame;
}
}
public class WebBrowserEvent
{
public bool cancel;
} public class WebbrowserOnNewWindow
{
static readonly BindingFlags info =
BindingFlags.Instance |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.FlattenHierarchy |
BindingFlags.CreateInstance; private WebBrowser web;
public WebbrowserOnNewWindow(WebBrowser webBrowser)
{
web = webBrowser ?? throw new Exception();
Excetue();
}
//跳转参考这个
public delegate void OnNewWindow(WebBrowserUrl2 webBrowserUrl, WebBrowserEvent webBrowserEvent); public event OnNewWindow BeforeNewWidnow; private void Excetue()
{ var propertyAxiWebbrowser = web.GetType().GetProperty("AxIWebBrowser2", info);
var axIWebBrowser2 = propertyAxiWebbrowser.GetValue(web, null);
var webBrowserEvent = new NewWebBrowserEvent(this);
var cookie = new ConnectionPointCookie(axIWebBrowser2, webBrowserEvent, typeof(INewDWWebBrowserEvent));
} public void OnBeforeNewWindow(string url, out bool cancel)
{
var Arg = BeforeNewWidnow;
WebBrowserUrl2 webBrowserUrl = new WebBrowserUrl2(url, null);
WebBrowserEvent webBrowserEvent = new WebBrowserEvent();
Arg?.Invoke(webBrowserUrl, webBrowserEvent);
cancel = webBrowserEvent.cancel;
}
} public class NewWebBrowserEvent : System.Runtime.InteropServices.StandardOleMarshalObject, INewDWWebBrowserEvent
{
private WebbrowserOnNewWindow web;
public NewWebBrowserEvent(WebbrowserOnNewWindow webbrowserOnNewWindow) => web = webbrowserOnNewWindow; public void BeforeNavigate2(object pDisp, ref object urlObject, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
{
//WPF这个和Winform差不多 没写
//webBrowser.OnBeforeNavigate((string)urlObject, (string)targetFrameName, out cancel);
}
//官方说明此 事件是低于IE6时会引发
public void NewWindow2(ref object ppDisp, ref bool cancel)
{
//WPF这个和Winform差不多 没写
// webBrowser.OnBeforeNewWindow(((WebBrowser)ppDisp).Url.ToString(), out cancel);
}
//当高于IE6时使用
public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
{
web.OnBeforeNewWindow((string)URL, out cancel);
}
} [System.Runtime.InteropServices.ComImport(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
System.Runtime.InteropServices.InterfaceTypeAttribute(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch),
System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)]
public interface INewDWWebBrowserEvent
{
[System.Runtime.InteropServices.DispId()]
void BeforeNavigate2(object pDisp, ref object urlObject, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel);
//官方说明此 事件是低于IE6时会引发
[System.Runtime.InteropServices.DispId()]
void NewWindow2(ref object ppDisp, ref bool cancel);
//当高于IE6时使用
[System.Runtime.InteropServices.DispId()]
void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL);
}
}

XAML 界面

//省略其他

<Grid>
<WebBrowser x:Name="WB" HorizontalAlignment="Left" Height="" Margin="10,10,0,0" VerticalAlignment="Top" Width="" Source="http://www.baidu.com" LoadCompleted="WB_LoadCompleted" />
</Grid>

XAML.CS

  public partial class MainWindow : Window
{
private bool Start;
public MainWindow()
{
InitializeComponent(); }
private void WB_LoadCompleted(object sender, NavigationEventArgs e)
{
if (!Start)
{ //这一步是没法省了
//webbrowser在WPF中时密封类型
WebbrowserOnNewWindow onNewWindow = new WebbrowserOnNewWindow(WB);
onNewWindow.BeforeNewWidnow += OnNewWindow_BeforeNewWidnow;
Start = true;
}
}
private void OnNewWindow_BeforeNewWidnow(WebBrowserUrl2 webBrowserUrl, WebBrowserEvent webBrowserEvent)
{
WB.Navigate(new Uri(webBrowserUrl.Url));
webBrowserEvent.cancel = true;
} }
05-12 19:54