Search code examples
c#dllnavigationchromiumcefsharp

Chromium webBrowser navigation methods


I have added a Chromium WebBrowser to my C# application using the CefSharp library, and now want to add functionality to a 'Back' button, that will allow the user to navigate to the previous page.

As I understand, this would be done with the following line:

browser.WebBrowser.Back();

However, if I just write this line in the method that is called when the 'Back' button is pressed, then an exception is thrown, and my application breaks.

The exception says:

Exception was unhandled

An unhandled exception of type 'System.Exception' occurred in CefSharp.dll

Additional information: lBrowser instance is null. Browser has likely not finished initializing or is in the process of disposing.

Why am I getting this exception? If I hover my cursor over the line that the exception occurs on, a message pops up which says:

(extension) void lWebBrowser.Back()

Navigates back, must check lWebBrowser.CanGoBack before calling this method.

But, if I then move this line inside an if statement that checks the value of lWebBrowser.CanGoBack first, i.e.

if(browser.WebBrowser.CanGoBack){
    browser.WebBrowser.Back();
}

then although my application will run, CanGoBack always appears to be false- any time I press the 'Back' button, the debug shown in my console indicates that it is false... but if try to assign it a value of true anywhere (i.e. when the user navigates to another page- so there is a page to go back to), for example:

browser.WebBrowser.CanGoBack = true;

I get a compile error which says:

Property or indexer 'lWebBrowser.CanGoBack' cannot be assigned to -- it is read only

So how can I force the browser to navigate back a page when the user clicks the 'back' button?


Solution

  • So it appears that the issue was to do with the version of CefSharp that I had installed/ the version I was referencing- it appears I had installed and was referencing the latest version, but the compiler seemed to think I was referencing an older version, and couldn't decide which one it thought it should use. After uninstalling CefSharp, removing all of the references I had made to it, and reinstalling it/ re-implementing my uses of it, my application now uses the browser correctly, and I am able to display and fully interact with the webpage I am displaying.