CefSharp prohibits the pop-up of a new form, opens a link in the same window, and supports a link with type = "POST" target = "_ blank", cefsharp_blank
1. Implement the ILifeSpanHandler interface. To support links with type = "POST" target = "_ blank", and to obtain ChromiumWebBrowser and release the old ChromiumWebBrowser, the new form still needs to be played, but it will be hidden later. The Code is as follows:
Using CefSharp; using CefSharp. winForms; using System. collections. generic; using System. linq; using System. text; using System. threading; using System. threading. tasks; using System. windows. forms; namespace MyBrowser {public class CefLifeSpanHandler: CefSharp. ILifeSpanHandler {public CefLifeSpanHandler () {} public bool DoClose (IWebBrowser browserControl, CefSharp. ibrow.browser) {if (brows Er. isDisposed | browser. isPopup) {return false;} return true;} public void OnAfterCreated (IWebBrowser browserControl, ibrow.browser) {} public void OnBeforeClose (IWebBrowser browserControl, ibrow.browser) {} public bool OnBeforePopup (IWebBrowser browserControl, ibrow.browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGe Sture, IWindowInfo windowInfo, ref bool browser, out IWebBrowser newBrowser) {var chromiumWebBrowser = (ExtChromiumBrowser) browserControl; ExtChromiumBrowser extBrowser = new ExtChromiumBrowser (targetUrl) {Dock = DockStyle. fill}; var windowX = windowInfo. x; var windowY = windowInfo. y; var export wwidth = (export winfo. width = int. minValue )? 600: Export winfo. Width; var export wheight = (export winfo. Height = int. MinValue )? 800: windowInfo. height; chromiumWebBrowser. invoke (new Action () =>{ var popup = new Form {Left = windowX, Top = windowY, Width = 1_wwidth, Height = running wheight, Text = targetFrameName}; popup. createControl (); var control = new Control (); control. dock = DockStyle. fill; control. createControl (); popup. controls. add (control); popup. show (); var rect = control. clientRectangle; windowInfo. setAsChild (control. handle, rect. left, rect. top, rect. right, rect. bottom); newmediaweventargs e = new newmediaweventargs (extBrowser, targetUrl, popup); chromiumWebBrowser. onNewWindow (e) ;}); newBrowser = null; return false ;}}}View Code
2. Use the ILifeSpanHandler interface:
Using CefSharp. winForms; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms. integration; namespace MyBrowser {public class ExtChromiumBrowser: ChromiumWebBrowser {public ExtChromiumBrowser (): base ("about: _ blank") {this. lifeSpanHandler = new CefLifeSpanHandler (); this. downloadHandler = new DownloadHandler (this ); } Public ExtChromiumBrowser (string adress): base (adress) {this. lifeSpanHandler = new CefLifeSpanHandler (); this. downloadHandler = new DownloadHandler (this);} public event EventHandler <newjavasweventargs> StartNewWindow; public void OnNewWindow (newjavasweventargs e) {if (StartNewWindow! = Null) {StartNewWindow (this, e );}}}}View Code
3. Define newjavasweventargs:
Using CefSharp; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. windows. forms; namespace MyBrowser {public class newjavasweventargs: EventArgs {private ExtChromiumBrowser _ browser; public ExtChromiumBrowser Browser {get {return _ browser;} set {value = _ browser ;}} private string _ url; public string Url {get {return _ url;} set {value = _ url;} private Form _ form; public Form {get {return _ form;} set {value = _ form;} public newjavasweventargs (ExtChromiumBrowser browser, string url, Form form) {_ url = url; _ browser = browser; _ form = form ;}}}View Code
4. Implement StartNewWindow. Three things are done in the StartNewWindow event. One is to replace the old ChromiumWebBrowser, the other is to release the old ChromiumWebBrowser, and the third is to hide the new pop-up form, the actual result may be that the icon in the Windows taskbar disappears, that is, the new pop-up form is hidden. This implementation method is abnormal, but if a new form is not allowed to pop up, the link with type = "POST" target = "_ blank" is not supported because the data is not POST to the server, if the new form is popped up, but the Control in the new form is used as the Child instead of a new ChromiumWebBrowser, the new ChromiumWebBrowser cannot be found. Although it can be normally displayed, it cannot be obtained, cannot be released later.
Private void Window_Loaded (object sender, RoutedEventArgs e) {_ browser = new ExtChromiumBrowser () {Dock = DockStyle. fill}; BindBrowser (_ browser); _ browser. frameLoadEnd + = FrameLoadEnd; _ browser. load (_ tokenUrl); host. child = _ browser;} private void BindBrowser (ExtChromiumBrowser browser) {browser. startNewWindow + = (s, e) => {this. dispatcher. beginInvoke (new Action () => {_ browser = e. browser; BindBrowser (e. browser); host. child = e. browser; e. browser. frameLoadStart + = (ss, ee) => {this. dispatcher. beginInvoke (new Action () =>{ (ss as ExtChromiumBrowser ). focus () ;}) ;}; e. form. visible = false; Task. factory. startNew () => {Thread. sleep (1000); this. dispatcher. beginInvoke (new Action () => {e. form. dispose (); browser. dispose ();}));});}));};}View Code