RaelB
RaelB

Reputation: 3481

How to create CefSharp ChromiumWebBrowser at runtime in WPF

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.

Upvotes: 4

Views: 9980

Answers (1)

RaelB
RaelB

Reputation: 3481

I found this answered in a similar question:

Set Address property instead of calling Load:

web.Address = "https://www.google.com";

Upvotes: 4

Related Questions