0

I'm trying to load a website URL using RegisterJsObject in one of two cefsharp instances. The URL is received from the website (chromeBrowser1) however It wont update the window (chromeBrowser2). I tried to document the script as much as possible so it would be more easy to understand. I'm pretty sure somebody with general knowledge in C# will find my problem, I spend hours trying to find a solution but I just couldn't.

        private void Form1_Load(object sender, EventArgs e)
    {
        CefSettings settings = new CefSettings();
        Cef.Initialize(settings);

        chromeBrowser2 = new ChromiumWebBrowser("http://google.com") // Initializate Browser 2 FIRST ( This is the instance we want to update every X seconds using chromeBrowser2.Load("http://somewebsite.com"); later on )
        {
            Dock = DockStyle.Fill,

        };
        splitContainer1.Panel2.Controls.Add(chromeBrowser2);



        chromeBrowser1 = new ChromiumWebBrowser("http://127.0.0.1/") // Initializate Browser 1 - Main website - source of data and more
        {
            Dock = DockStyle.Fill,

        };
        splitContainer1.Panel1.Controls.Add(chromeBrowser1);
        chromeBrowser1.RegisterJsObject("callbackObj", new CallbackObjectForJs()); /// the link is obtained from here  - See picture
    }

    public class CallbackObjectForJs
    {
        private readonly static BackgroundWorker backgroundWorker1 = new BackgroundWorker();
        private readonly static ChromiumWebBrowser chromeBrowser2 = new ChromiumWebBrowser("http://facebook.com");
        // IF I REMOVE ANY OF THE ABOVE I WILL RECEIVE THE FOLLOWING ERROR : 
        // Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.backgroundWorker1' 
        // Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.chromeBrowser2'
        public void showMessage(string msg)
        {

            var sitelink = msg;

            Console.WriteLine(sitelink); // WORKING -  link is visible on console but chromeBrowser 2 is not update, still reads http://google.com
            Console.WriteLine("Loading site using one of the options below"); // Can be seen on console +

            // AT THIS POINT chromeBrowser2 SHOULD LOAD THE NEW URL ///

            chromeBrowser2.Load(sitelink); // NOT WORKING
            Console.WriteLine("Check Point 1");
            backgroundWorker1.RunWorkerAsync(sitelink); // NOT WORKING
            Console.WriteLine("Check Point 2");


        }

    }     

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) // NOT WORKING
    {

        Console.WriteLine("Background Worker"); // Nothing
        string link = (string)e.Argument;
        Console.WriteLine("Check Point 3");
        chromeBrowser2.Load(link);
        Console.WriteLine("Check Point 5");
        Console.WriteLine(link);
        Console.WriteLine("Check Point 6");

    }

This is what the console shows Console results.

I understand that, for some reason, neither of the option I've chosen are being triggered. I have high speculations that is because of Error CS0120, which I managed to hide with making them static but that's it pretty much. I'm out of ideas. Search results got me this far.

rgerculy
  • 481
  • 5
  • 14
  • 1
    Did you tried setting the Address property on Chromium browser object? – Ali Baig Jan 21 '17 at 18:57
  • Honestly, I'm not sure what you mean by that. If you mean if I set a default URL, then I did. Also, this is the full script. Everything I did is right there. – rgerculy Jan 21 '17 at 19:06
  • I see what you mean now, ChromiumWebBrowser.Address Property. No, I haven't. But I will do it now to see if it reveals something. – rgerculy Jan 21 '17 at 19:10
  • I added a Address bar and it stay on Google, it's not being updated. – rgerculy Jan 21 '17 at 19:25
  • Try using Reload after setting the property like ChromiumBrowser.Reload(true) – Ali Baig Jan 21 '17 at 20:07
  • The address bar is working properly. Default URL for the second instance is google.com right ? If I navigate on google the address bar is being update without issue. – rgerculy Jan 21 '17 at 20:12
  • You have three instances of ChromiumWebBrowser, pass the second as a parameter to your bound class and remove the static instance. Ditch the background worker, only complicates things. – amaitland Jan 22 '17 at 01:45
  • If I ditch the static instance I'm back to start. I can't use anything within showMessage or CallbackObjectForJs for that matter. Simple solution would be just using chromeBrowser2.Load(sitelink); as you said but I keep getting the same CS0120 error. – rgerculy Jan 22 '17 at 07:57

1 Answers1

0

I installed ReSharper and it found the problem in less than a minute. All I had to do is make chromeBrowser2 static.

    public partial class Form1 : Form
    {

    public ChromiumWebBrowser chromeBrowser1;
    public *static* ChromiumWebBrowser chromeBrowser2;
    ....

Thanks for any input.

rgerculy
  • 481
  • 5
  • 14
  • 1
    You should be passing a reference in to the constructor of your registered class, making the instance static may work, it's far from best practice. – amaitland Jan 22 '17 at 11:33
  • I will check that out but the application is pretty much that. There are no other tasks for it. Everything else will be processed in PHP. – rgerculy Jan 22 '17 at 11:56