I have an application with two tabs in a tab control, each containing a CefSharp ChromiumWebBrowser. Let's call these ChromiumWebBrowsers searchBrowser
and detailsBrowser
.
Let's say I click a link in searchBrowser
, and if that link contains the text "xrep2", I want the link to be opened in detailsBrowser
, else it should be opened in searchBrowser
.
In a previous version of this application, I used the WinForms web browser control. I'm rewriting the app in WPF and using CefSharp. with the WinForms it was quite simple. In the Navigating
event, you do something like
private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (browser.StatusText.Contains("xrep2."))
{
e.Cancel = true;
detailsBrowser.Url = new Uri(browser.StatusText);
tabControl1.SelectedIndex = 1;
}
}
But when using CefSharp, it appears I have to create an event inside a LifeSpanHandler per this post with an event that fires when a target="_blank"
link is clicked. I'm not super comfortable creating and subscribing to custom events, so I'd like to find an easier way to do this, but if not, I need to figure out how to subscribe a method to the event created by public event Action<string> PopupRequest;
and where to put the logic (something like if(searchBrowser.Address.Contains("xrep2")) detailsBrowser.Address = X else searchBrowser.Address = X
I have the LifeSpanHandler class code below, including the PopupRequest event that is supposed to fire when a target="_blank"
link is clicked, but I'm stuck at this point.
public class LifeSpanHandler : ILifeSpanHandler
{
public event Action<string> PopupRequest;
public virtual bool OnBeforePopup(IWebBrowser browserControl, string sourceUrl, string targetUrl, ref int x, ref int y, ref int width,
ref int height)
{
if (PopupRequest != null)
PopupRequest(targetUrl);
return true;
}
}