CefSharp WPF web browser is not displayed Or rendered
Asked Answered
L

4

9

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

Lurdan answered 13/2, 2015 at 12:5 Comment(0)
B
9

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/");
    }
}
Brag answered 21/10, 2016 at 15:47 Comment(0)
D
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.

Ducharme answered 18/2, 2015 at 5:57 Comment(2)
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.Cusk
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.Ducharme
W
0

Have you tried replacing

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

with

webView.Load("http://www.google.co.uk");
Wenwenceslaus answered 13/2, 2015 at 16:34 Comment(2)
Yes, I have tried with webView.Load("google.co.uk"); But still I could not get this done.Lurdan
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.Wenwenceslaus
T
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.

Tracey answered 26/2, 2015 at 8:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.