WPF CefSharp web browser is not displayed or displayed

New to CefSharp I created a class library project and referenced the CefSharp library to display a web browser. However, I have to face some issues showing a 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 reference 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, by clicking on the "I" button, I will open a child widget (WebBrowser_test1), in which I will show a web browser. When the window opens, it will be empty.

Please let me know if I missed something.

+5
source share
4 answers

You tried to replace

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

from

 webView.Load("http://www.google.co.uk"); 
0
source

I can think of the first three potential problems. But it’s hard to say what the real problem is from your code, as it deviates a bit from official examples.

  • Move Cef.Initialize() to the MainWindow constructor. It needs to be called only once to start the CefSharp.BrowserSubprocess.exe renderer process.

  • See my answer on CefSharp 3 always refuses Cef.Initialize () for a few things to check with regard to binary files and their placement. Indeed, the recommended approach is to start with a WPF example in the CefSharp.MinimalExample repository, and then adapt to your use case.

  • I'm not sure if ChromiumWebBrowser() without explicitly setting the width and height. Window 0x0 may not receive rendered content. I have not tried with the latest code.

0
source

As jornh mentions, you may need to explicitly set the height and width of the ChromiumWebBrowser . If you do not know the exact size, the HorizontalAlignment and VerticalAlignment settings on the Stretch may also work (to fill the parent container).

Have you checked if Cef.Initialize() really returns true ? You may be missing some files, and CefSharp does not always give clear error messages when this is the case.

0
source

Subscribe to IsBrowserInitializedChanged after creating ChromiumWebBrowser . Then, as soon as 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/"); } } 
0
source

Source: https://habr.com/ru/post/1213316/


All Articles