标签:cef xilium.cefglue在当 xilium.cefglue防止弹窗
我们在使用Xilium.CefGlue编写浏览器应用程序时,对于嵌入的网页如果有链接会在新窗口打开,这样的用户体验会很差,因此我们需要修改程序,使所有链接都在当前窗口中打开。
首先引用Xilium.CefGlue.dll文件,需要继承Xilium.CefGlue 中的CefLifeSpanHandler类
namespace Xilium.CefGlue { // 摘要: // Implement this interface to handle events related to browser life span. The // methods of this class will be called on the UI thread unless otherwise indicated. public abstract class CefLifeSpanHandler
继承CefLifeSpanHandler类,并重写OnBeforePopup方法,实现如下:
internal sealed class CwbLifeSpanHandler : CefLifeSpanHandler { private ChromeWebBrowser webBrowser; public CwbLifeSpanHandler(ChromeWebBrowser browser) { webBrowser = browser; } protected override void OnAfterCreated(CefBrowser browser) { base.OnAfterCreated(browser); webBrowser.OnCreated(browser); } protected override bool DoClose(CefBrowser browser) { return false; } protected override bool OnBeforePopup(CefBrowser browser, CefFrame frame, string targetUrl, string targetFrameName, CefWindowOpenDisposition targetDisposition, bool userGesture, CefPopupFeatures popupFeatures, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings, ref bool noJavascriptAccess) { bool res = false; if (!string.IsNullOrEmpty(targetUrl)) { if (webBrowser.selfRequest != null) { CefRequest req = CefRequest.Create(); req.FirstPartyForCookies = webBrowser.selfRequest.FirstPartyForCookies; req.Options = webBrowser.selfRequest.Options; System.Collections.Specialized.NameValueCollection h = new System.Collections.Specialized.NameValueCollection(); h.Add("Content-Type", "application/x-www-form-urlencoded"); req.Set(targetUrl, webBrowser.selfRequest.Method, null, webBrowser.selfRequest.GetHeaderMap()); webBrowser.selfRequest = req; } //在该处实现用当前页面打开所有新开的窗口 webBrowser.OpenUrl(targetUrl); res = true; if (res) return res; } return base.OnBeforePopup(browser, frame, targetUrl, targetFrameName, targetDisposition, userGesture, popupFeatures, windowInfo, ref client, settings, ref noJavascriptAccess); return res; } }
public sealed class ClientBrowser : CefClient { private readonly CwbLifeSpanHandler _lifeSpanHandler; private ChromeWebBrowser webBrowser; public ClientBrowser(ChromeWebBrowser browser) { webBrowser = browser; _lifeSpanHandler = new CwbLifeSpanHandler(browser); } public override CefLifeSpanHandler GetLifeSpanHandler() { return _lifeSpanHandler; } }
到此我们的工作就已经完成,可以直接运行程序进行测试了
版权声明:本文为博主原创文章,未经博主允许不得转载。
CEF Xilium.CefGlue 在当前窗口中打开所有链接(防止弹窗)
标签:cef xilium.cefglue在当 xilium.cefglue防止弹窗
原文地址:http://blog.csdn.net/zhang116868/article/details/46945973