简体   繁体   中英

CEFSharp Open New window

Alright all,

I have stumped myself, spent the last few days searching and trying to find a solution with no luck.

Running latest version of CefSharp, coding c# and vuejs. I am able to easily get Form1 to display my main webpage though localhost. Setup and initialization of the chromium browser is fine. I am able to communicate from the web page back to c# through

browser.JavascriptObjectRepository.Register("bBrowser", new JSInterface(), true);

On the javascript side I have the following

async openStandings(){
  await bBrowser.kappsopenStandings().then((result1)=>{ 
      this.filler = result1

       return result1                 
})
},

The above code calls the following c# function in the jsInterface class

   public bool kappsopenStandings(
        )
    {

        bool m = false;


        Standings S = new Standings();

        S.Show();


        m = true;
        return m;
    }

The above Standings class is a form which has the following Load function and InitBrowser function.

private  void Standings_Load(object sender, EventArgs e)
    {
        this.StartPosition = FormStartPosition.CenterScreen;

        InitBrowser();
    }

and the InitBrowser function:

    public void InitBrowser()
    {

       standingsbrowser = new ChromiumWebBrowser(thelink);

        Dock = DockStyle.Fill;
        this.Controls.Add(standingsbrowser);

        Console.WriteLine("Created Window");
    }

Ok, so what happens is the standings windows is created and everything freezes and hangs.

Now, through my research I understand that when I call from the webpage I am not impacting the UI thread which seems to be where my issue is.

If I place an actual c# button on the form and click it all is fine. I have tried to PerformClick() on a visible=false button on Form1 with the same results as above.

Could someone point me in the right direction for creating multiple winforms each with there own browse?

Thank you,

Kory

You need to open your form on the WinForms UI Thread , an example would look roughly like below (I wrote this in Notepad++ so I haven't tested that it compiles, you should get the basic idea).

public class JSInterface
{
    private Form _form;

    public JSInterface(Form form)
    {
        _form = form;
    }

    public bool kappsopenStandings()
    {
        //Marshal onto the WinForms UI Thread
        _form.BeginInvoke(() =>
        {
            var standingsForm = new Standings();

            standingsForm.Show();
        });

        return true;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM