CefSharp WPF web browser is not displayed Or rendered

Krishna Kumar picture Krishna Kumar · Feb 13, 2015 · Viewed 13.5k times · Source

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

Answer

jltrem picture jltrem · Oct 21, 2016

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/");
    }
}