Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create CefSharp ChromiumWebBrowser at runtime in WPF

Tags:

c#

wpf

cefsharp

I have followed this tutorial that shows how to get started with CefSharp in WPF.

The control is declared in xaml:

<Window x:Class="Sample1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        Title="MainWindow" Height="550" Width="625">
    <Grid>
        <cefSharp:ChromiumWebBrowser Grid.Row="0"
        Address="https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-questions" />
  </Grid>
</Window>

How can I achieve the same by creating the ChromiumWebBrowser at runtime?

I have tried something like this:

    public partial class MainWindow : Window
    {
        ChromiumWebBrowser web;

        public MainWindow()
        {
            InitializeComponent();

            web = new ChromiumWebBrowser();
            web.Load("https://www.google.com");
            Grid.SetRow(web, 0);
            Grid1.Children.Add(web);
        }

    }

XAML:

<Window x:Class="CefSharpWPFDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CefSharpWPFDemo"
        xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="434.607" Width="656.899" WindowStartupLocation="CenterScreen">
    <Grid x:Name="Grid1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
    </Grid>
</Window>

But the window is blank.

like image 675
RaelB Avatar asked Sep 06 '25 15:09

RaelB


1 Answers

I found this answered in a similar question:

Set Address property instead of calling Load:

web.Address = "https://www.google.com";
like image 74
RaelB Avatar answered Sep 09 '25 08:09

RaelB