8

Am new to CefSharp I have created a class library project and referenced the CefSharp library to render the Web browser, However I am facing some issues showing the web Browser. Please find the exact code

WebBrowser_test1:

 public partial class ChildWidget : Window
    {
        public CefSharp.Wpf.ChromiumWebBrowser webView;
        public Widget()
        {
            InitializeComponent();
            CefSharp.CefSettings settings = new CefSharp.CefSettings();
            settings.PackLoadingDisabled = true;
            if (CefSharp.Cef.Initialize(settings))
            {
                webView = new CefSharp.Wpf.ChromiumWebBrowser();
                main_grid.Children.Add(webView);
                webView.Address = "http://www.google.co.uk";
            }
        }
    }

and I am referencing this library (dll) in another project

public MainWindow()
        {
            InitializeComponent();
            Button newbutton = new Button();
            newbutton.Width = 50;
            main_grid.Children.Add(newbutton);
            newbutton.Click += ButtonClick;
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                Webbrowser_test1.ChildWidget childWidget = new Widget();
                childWidget.Show();
            }
            catch (Exception)
            {

                throw;
            }
          }

Now on the Button click I will open the (WebBrowser_test1) child widget in which I will show the web browser .. when the window opens it is showing blank.

Please let me know if I missing anything

jornh
  • 1,369
  • 13
  • 27
Krishna Kumar
  • 81
  • 1
  • 1
  • 4

4 Answers4

8

Subscribe to IsBrowserInitializedChanged after creating a ChromiumWebBrowser. Then once the browser is initialized you can call Load and your control will be displayed.

...
    _browser = new ChromiumWebBrowser();
    mainGrid.Children.Add(_browser);
    _browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
...

void OnIsBrowserInitializedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
    if (_browser.IsBrowserInitialized)
    {
        _browser.Load("https://www.google.com/");
    }
}
jltrem
  • 12,124
  • 4
  • 40
  • 50
1

I can think of the first three potential issues. But it's hard to tell what the real issue is from your code alone as it strays off a bit from the official examples

  1. Move Cef.Initialize() to your MainWindow constructor. It should only be called once to launch the CefSharp.BrowserSubprocess.exe renderer process.

  2. See my answer to CefSharp 3 always failing Cef.Initialize() for a few things to check regarding binaries and their placement. Really, the recommended approach is to start having the WPF example in the CefSharp.MinimalExample repo running first and then adjust to your use case from there.

  3. I'm not sure a ChromiumWebBrowser() without explicitly setting a width and height works. A 0x0 window might not receive any rendered content. I haven't tried with recent code.

Community
  • 1
  • 1
jornh
  • 1,369
  • 13
  • 27
  • Hi there @Jornh, I am having a similar problem but when I run the aplication on visual studio it works perfectly but when I publish, it keeps loading a blank page instead of the regular UI. I have also looked about how to publish a click once application, but it seems to be correct. Do you have any tips for me? ive beeing struggling with this a few day now. – Ana Sep 24 '20 at 15:06
  • Sorry it’s been a few years since I’ve worked with CefSharp. Only thing I can suggest is the usual stuff to maybe look for missing DLLs in your deployment and usual stuff like errors if you can get a debugger attached. – jornh Sep 25 '20 at 17:52
0

Have you tried replacing

webView.Address = "http://www.google.co.uk";

with

webView.Load("http://www.google.co.uk");
penderi
  • 8,673
  • 5
  • 45
  • 62
  • Yes, I have tried with webView.Load("http://www.google.co.uk"); But still I could not get this done. – Krishna Kumar Feb 16 '15 at 04:08
  • Well this in general absolutely works, and cefsharp is wonderful, so I think it's the slightly exotic 'control' in a 'control' usage you're going for. My instinct says it's a lifetime issue. I'd advise against calling Load (as you say you're now calling) in the control constructor. You need to let CEF load the dll ( ~20Mb ) into memory first and intialise. There's an IsBrowserInitialised event that can help there. I'd also advise creating a new project, without using controls, that simply loads and CEFSharp and displays a webpage to familiarize yourself with cef. – penderi Feb 16 '15 at 08:30
0

Like jornh mentions, you may have to explicitly set the height and width of the ChromiumWebBrowser. If you don't know the exact size, setting HorizontalAlignment and VerticalAlignment to Stretch (to fill the parent container) will probably also work.

Have you checked if the Cef.Initialize() actually returns true? You could be missing some files, and CefSharp doesn't always give clear error messages when this is the case.

Desarc
  • 121
  • 5