Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CefSharp WPF web browser is not displayed Or rendered

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

like image 222
Krishna Kumar Avatar asked Feb 13 '15 12:02

Krishna Kumar


2 Answers

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/");
    }
}
like image 149
jltrem Avatar answered Nov 11 '22 23:11

jltrem


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.

like image 1
jornh Avatar answered Nov 12 '22 01:11

jornh