如何禁止在新 window 中禁用打开的页面 - cefsharp

how to disallow disable open page in new window - cefsharp

我已经尝试了很多但无法让它工作:(。 我想让第二个 window 在同一个主 window 中打开,而不是在新的 window.

中打开

我试过跟随但没有运气,也许我做错了什么,因为我不擅长编码:(

https://github.com/cefsharp/CefSharp/issues/600

https://github.com/cefsharp/CefSharp/issues/688

https://www.codeproject.com/Articles/1194609/Capturing-a-pop-up-window-using-LifeSpanHandler-an

你能帮我找到解决办法吗,无论你在任何网页上点击什么按钮,它都只会在 Window 中打开

using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;
using Microsoft.Win32;


namespace Nysus
{

  
public partial class Form1 : Form
    {
        public Form1()
        {
             InitializeComponent();
        }

        ChromiumWebBrowser chrome;
        private void Form1_Load(object sender, EventArgs e)
        {
            CefSettings settings = new CefSettings();
            //Initialize
            Cef.EnableHighDPISupport();
            Cef.Initialize(settings);
            chrome = new ChromiumWebBrowser("http://www.google.com");
            this.pContainer.Controls.Add(chrome);
            chrome.Dock = DockStyle.Fill;
          
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            if (chrome.CanGoForward)
                chrome.Forward();
            if (chrome.CanGoBack)
                chrome.Back();
        }
       
        private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }

        public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
        {
            browser.MainFrame.LoadUrl(targetUrl);
            newBrowser = null;
            return true;
        }

    }
}

如果您想要强制每个目标空白 link 在同一浏览器中打开 window,LifeSpanHandler 是可行的方法(考虑到您无权访问来源 HTML)。 OnBeforePopup 方法在这种情况下有效,但您在无意义的地方添加了该方法。创建一个扩展 ILifeSpanHandler class:

的新 class
// MyCustomLifeSpanHandler.cs
using CefSharp;

public class MyCustomLifeSpanHandler : ILifeSpanHandler
{
    // Load new URL (when clicking a link with target=_blank) in the same frame
    public bool OnBeforePopup(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser)
    {
        browser.MainFrame.LoadUrl(targetUrl);
        newBrowser = null;
        return true;
    }
} 

然后在浏览器初始化期间用自定义的 LifeSpanHandler 替换(您的代码应该是):

ChromiumWebBrowser chrome;
private void Form1_Load(object sender, EventArgs e)
{
    CefSettings settings = new CefSettings();
    //Initialize
    Cef.EnableHighDPISupport();
    Cef.Initialize(settings);
    chrome = new ChromiumWebBrowser("http://www.google.com");

    // Implement Custom LifeSpanHandler
    chrome.LifeSpanHandler = new MyCustomLifeSpanHandler();
    // End Implement Custom LifeSpanHandler

    this.pContainer.Controls.Add(chrome);
    chrome.Dock = DockStyle.Fill;
    
    this.FormBorderStyle = FormBorderStyle.None;
    this.WindowState = FormWindowState.Maximized;
    if (chrome.CanGoForward)
        chrome.Forward();
    if (chrome.CanGoBack)
        chrome.Back();
}

这应该行得通。您可以找到 whole example in this article here.