我尝试在Cefsharp(版本43,Winforms)中处理弹出窗口,因为我需要在弹出窗口中执行一些javascript并导航到其他站点,但是当弹出窗口出现时应用程序开始冻结。另外,我从这个问题How to handle popup links in CefSharp中读到了答案,我试图实现它,我的代码看起来像这样。
public class LifeSpanHandler : ILifeSpanHandler
{
public event Action<string> PopupRequest;
//The other members of this interface I leave empty
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)
{
if (PopupRequest != null)
PopupRequest(targetUrl);
newBrowser = browserControl; // here I think is a problem
return true;
}
}
// Here is Form1 class
private void Button1_Click(object sender, EventArgs e)
{
LifeSpanHandler life = new LifeSpanHandler();
chromiumWebBrowser1.LifeSpanHandler = life;
life.PopupRequest += life_PopupRequest;
chromiumWebBrowser1.Load("http://www.popuptest.com/goodpopups.html");
}
void life_PopupRequest(object sender, string e)
{
Popup pop = new Popup();
ChromiumWebBrowser popBrowser = new ChromiumWebBrowser(e);
pop.Controls.Add(popBrowser);
pop.Visible = true;
popBrowser.Dock = DockStyle.Fill;
popBrowser.Visible = true;
// here the Application is freezing
}`
那么如何控制弹出窗口(执行一些javascript并导航到其他站点)?