C# CefSharp DevTool 的关闭按钮不起作用
C# CefSharp DevTool's close button does not work
我在CefSharp浏览器中使用Devtools添加了以下代码来检查元素,但是打开Devtools后,关闭按钮不起作用
public class DevToolsMenuHandler : IContextMenuHandler
{
public void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
IMenuModel model)
{
model.AddItem(CefMenuCommand.CustomFirst, "Inspect");
}
public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
CefMenuCommand commandId, CefEventFlags eventFlags)
{
if (commandId != CefMenuCommand.CustomFirst) return false;
browser.ShowDevTools(null, parameters.XCoord, parameters.YCoord);
return true;
}
public void OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
}
public bool RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
由于我的 CefSharp 浏览器名为 'chrome',我尝试将 'iBrowser browser' 更改为 'iBrowser chrome',但没有帮助
public partial class Form1 : Form
{
public ChromiumWebBrowser chrome;
public CefSettings settings;
string main_site = "example.com";
public Form1()
{
InitializeComponent();
InitBrowser();
}
public void InitBrowser()
{
settings = new CefSettings();
Cef.Initialize(settings);
textBoxUrl.Text = main_site;
chrome = new ChromiumWebBrowser(textBoxUrl.Text);
chrome.MenuHandler = new DevToolsMenuHandler();
this.panel1.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;
chrome.AddressChanged += Chrome_AddressChanged;
}
我也有class“MyCustomLifeSpanHandler”在这个class有“Webbrowser chromium Web Browser, Browser browser,etc”这些arguments/fiels也存在于class “DevToolsMenuHandler”。
可能结果是名称冲突?
我如何组合这些 classes?
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;
}
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
// throw new NotImplementedException();
return true;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
// throw new NotImplementedException();
}
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
// throw new NotImplementedException();
}
}
如何组合这些 classes?
尝试安装最新的 100-pre 版本。它应该在不同的版本中工作。当你遇到此类问题时,尝试在不同版本的 CefSharp
中测试你的代码
您的 ILifeSpanHandler
实施有问题,您正在阻止关闭 DevTools Popup
window。
您需要为 Popups
允许关闭 (return false
)。与其直接实现 ILifSpanHandler
,我建议从 CefSharp.Handler.LifeSpanHandler
继承并覆盖你需要的方法。
chrome.LifeSpanHandler = new CustomLifeSpanHandler();
public class CustomLifeSpanHandler : CefSharp.Handler.LifeSpanHandler
{
protected override bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
if(browser.IsPopup)
{
return false;
}
return true;
}
protected override 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;
}
}
我在CefSharp浏览器中使用Devtools添加了以下代码来检查元素,但是打开Devtools后,关闭按钮不起作用
public class DevToolsMenuHandler : IContextMenuHandler
{
public void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
IMenuModel model)
{
model.AddItem(CefMenuCommand.CustomFirst, "Inspect");
}
public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
CefMenuCommand commandId, CefEventFlags eventFlags)
{
if (commandId != CefMenuCommand.CustomFirst) return false;
browser.ShowDevTools(null, parameters.XCoord, parameters.YCoord);
return true;
}
public void OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
{
}
public bool RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters,
IMenuModel model, IRunContextMenuCallback callback)
{
return false;
}
}
由于我的 CefSharp 浏览器名为 'chrome',我尝试将 'iBrowser browser' 更改为 'iBrowser chrome',但没有帮助
public partial class Form1 : Form
{
public ChromiumWebBrowser chrome;
public CefSettings settings;
string main_site = "example.com";
public Form1()
{
InitializeComponent();
InitBrowser();
}
public void InitBrowser()
{
settings = new CefSettings();
Cef.Initialize(settings);
textBoxUrl.Text = main_site;
chrome = new ChromiumWebBrowser(textBoxUrl.Text);
chrome.MenuHandler = new DevToolsMenuHandler();
this.panel1.Controls.Add(chrome);
chrome.Dock = DockStyle.Fill;
chrome.AddressChanged += Chrome_AddressChanged;
}
我也有class“MyCustomLifeSpanHandler”在这个class有“Webbrowser chromium Web Browser, Browser browser,etc”这些arguments/fiels也存在于class “DevToolsMenuHandler”。 可能结果是名称冲突? 我如何组合这些 classes?
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;
}
public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
// throw new NotImplementedException();
return true;
}
public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
// throw new NotImplementedException();
}
public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
// throw new NotImplementedException();
}
}
如何组合这些 classes?
尝试安装最新的 100-pre 版本。它应该在不同的版本中工作。当你遇到此类问题时,尝试在不同版本的 CefSharp
中测试你的代码您的 ILifeSpanHandler
实施有问题,您正在阻止关闭 DevTools Popup
window。
您需要为 Popups
允许关闭 (return false
)。与其直接实现 ILifSpanHandler
,我建议从 CefSharp.Handler.LifeSpanHandler
继承并覆盖你需要的方法。
chrome.LifeSpanHandler = new CustomLifeSpanHandler();
public class CustomLifeSpanHandler : CefSharp.Handler.LifeSpanHandler
{
protected override bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser)
{
if(browser.IsPopup)
{
return false;
}
return true;
}
protected override 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;
}
}