如何在CefSharp中处理弹出链接 [英] How to handle popup links in CefSharp
问题描述
我正在使用 CefSharp 39.0.2
创建一个选项卡式Web浏览器。现在,如果用户单击网站上的链接,它将打开一个新窗口,该窗口没有我的原始UI。例如,当您单击Google新闻上的文章链接时,该链接会在新窗口中打开,但是没有我已设置的任何浏览控件。我还研究了 Cef.WinForms.Example
程序,它执行的操作完全相同。
I am creating a tabbed web browser using CefSharp 39.0.2
. Right now, if the user clicks on a link on a website, it will open a new window that has none of my original UI. For example, when you click on an article link on Google News, it opens in a new window, but without any of the browsing controls I have made up. I also looked into the Cef.WinForms.Example
program, and it does the same exact thing.
是否可以用其他方式处理?我希望该链接在新选项卡中打开,或者在新窗口中打开(所有控件都在其中)。我一直在研究GitHub问题,但找不到类似的东西(也许我不够努力,因为我认为这也是其他人想要做的……)。我浏览了所有事件
中的浏览器控件,但找不到任何看起来像它们处理的事件。
Is it possible to handle this in a different way? I would like for the link to either open in a new tab, or open in a new window (with all the controls there). I have been looking through the GitHub issues, and I could not find anything like this (maybe I was not looking hard enough, because I would think this is what others want to do as well...). I looked through all the events
for the browser control, and I could not find any that would seem like they handle it.
推荐答案
ChromiumWebBrowser
具有 LifeSpanHandler
属性。要在Cefsharp中手动控制弹出窗口,您必须实现自己的生命周期处理程序对象,并实现 ILifeSpanHandle
接口。
ChromiumWebBrowser
has a LifeSpanHandler
property. To manually control popup windows in Cefsharp, you have to implement your own life span handler object implementing the ILifeSpanHandle
interface.
每次浏览器要打开一个新窗口时,它将调用您的生命周期处理程序的 OnBeforePopup
函数。在这里您可以实现所需的行为。如果返回 false
,浏览器将弹出一个新窗口。如果返回 true
,浏览器将忽略弹出操作,但是您可以手动创建新窗口,新选项卡等。
Every time browser wants to open a new window, it will call your life span handler's OnBeforePopup
function. Here you can implement your desired behavior. If you return false
, browser will pop up a new window. If you return true
, browser will ignore pop up action, but you can manually create your new window, new tab, etc...
这是自定义寿命处理程序的非常简单的示例。在弹出请求时,它将触发一个名为PopupRequest的事件。您可以订阅此类事件并手动创建新的窗口/选项卡。然后,它返回true,指示 ChromiumWebBrowser
不要创建自己的新窗口。但是,您需要自己创建一个具有另一个 ChromiumWebBrowser
的新窗口。
This is a very simple example of custom life span hander. On popup request, it fires an event called PopupRequest. You can subscribe such event and create new window/tab manually. Then, it returns true that instructs ChromiumWebBrowser
not to create its own new window. However, you need to implement creating new window with another ChromiumWebBrowser
on your own.
public class SampleLifeSpanHandler: ILifeSpanHandler
{
public event Action<string> PopupRequest;
public bool OnBeforePopup(IWebBrowser browser, string sourceUrl, string targetUrl, ref int x, ref int y, ref int width,
ref int height)
{
if (PopupRequest != null)
PopupRequest(targetUrl);
return true;
}
public void OnBeforeClose(IWebBrowser browser)
{
}
}
这篇关于如何在CefSharp中处理弹出链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!