CefSharp禁止弹出新窗体,在同一窗口打开链接,并且支持带type="POST" target="_blank"的链接

1、实现ILifeSpanHandler接口,代码如下:

using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyBrowser
{
    public class CefLifeSpanHandler : CefSharp.ILifeSpanHandler
    {
        public CefLifeSpanHandler()
        {

        }

        public bool DoClose(IWebBrowser browserControl, CefSharp.IBrowser browser)
        {
            if (browser.IsDisposed || browser.IsPopup)
            {
                return false;
            }

            return true;
        }

        public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser)
        {

        }

        public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser)
        {
        }

        public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IWindowInfo windowInfo, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            var chromiumWebBrowser = (ExtChromiumBrowser)browserControl;

            chromiumWebBrowser.Invoke(new Action(() =>
            {
                NewWindowEventArgs e = new NewWindowEventArgs(windowInfo);
                chromiumWebBrowser.OnNewWindow(e);
            }));

            newBrowser = null;
            return false;
        }

    }
}
View Code

2、使用ILifeSpanHandler接口:

using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.Integration;

namespace MyBrowser
{
    public class ExtChromiumBrowser : ChromiumWebBrowser
    {
        public ExtChromiumBrowser()
            : base(null)
        {
            this.LifeSpanHandler = new CefLifeSpanHandler();
            this.DownloadHandler = new DownloadHandler(this);
        }

        public event EventHandler<NewWindowEventArgs> StartNewWindow;

        public void OnNewWindow(NewWindowEventArgs e)
        {
            if (StartNewWindow != null)
            {
                StartNewWindow(this, e);
            }
        }
    }
}
View Code

3、定义NewWindowEventArgs:

using CefSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyBrowser
{
    public class NewWindowEventArgs : EventArgs
    {
        private IWindowInfo _windowInfo;
        public IWindowInfo WindowInfo
        {
            get { return _windowInfo; }
            set { value = _windowInfo; }
        }

        public NewWindowEventArgs(IWindowInfo windowInfo)
        {
            _windowInfo = windowInfo;
        }
    }
}
View Code

4、实现StartNewWindow:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    _browser = new ExtChromiumBrowser() { Dock = DockStyle.Fill };
    BindBrowser(_browser);
    _browser.FrameLoadEnd += FrameLoadEnd;
    _browser.Load(_tokenUrl);
    host.Child = _browser;
}

private void BindBrowser(ExtChromiumBrowser browser)
{
    browser.StartNewWindow += (s, e) =>
    {
        var control = new System.Windows.Forms.Control();
        control.Dock = DockStyle.Fill;
        control.CreateControl();
        host.Child = control;
        control.Focus();

        e.WindowInfo.SetAsChild(control.Handle, 0, 0, (int)host.ActualWidth, (int)host.ActualHeight);
    };
    browser.FrameLoadStart += (ss, ee) =>
    {
        this.Dispatcher.BeginInvoke(new Action(() =>
        {
            (ss as ExtChromiumBrowser).Focus();
        }));
    };
}
View Code

 

posted @ 2017-10-23 14:56  0611163  阅读(3840)  评论(10编辑  收藏  举报