'C# CefSharp DevTool's close button does not work
I added the following code to inspect the element in the CefSharp browser using Devtools, but after opening Devtools, the close button does not work
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;
}
}
Because of my CefSharp browser is called 'chrome', I tried to change 'iBrowser browser' to 'iBrowser chrome', but it didn't help
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;
}
I also have class "MyCustomLifeSpanHandler" in this class there are "Webbrowser chromium Web Browser, Browser browser, etc" these arguments/fiels are also present in the class "DevToolsMenuHandler". Probably it turns out a name conflict? How do I combine these 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();
}
}
How do I combine these classes?
Solution 1:[1]
Your ILifeSpanHandler implementation is the problem, you are blocking close of the DevTools Popup window.
You need to allow close (return false) for Popups. Rather than implement ILifSpanHandler directly I suggest inheriting from CefSharp.Handler.LifeSpanHandler and just override the methods you require.
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;
}
}
Solution 2:[2]
Try to install latest 100-pre release. It should work in a different version. When you encounter such problems, try to test your code in different versions of CefSharp
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | amaitland |
| Solution 2 | Vlad Yorkyee |
