Search code examples
c#user-controlschromium-embeddedcefsharp

Adding a Usercontrol with CefSharp to a WinForms app


I'm trying to create a user control with a CefSharp to be able to have a web browser with custom properties. The control works fine except when I try to add the control to a WebForms app with the Graphic Designer, every time I try to add the control graphically I get the same error:

Failed to create the component 'MyComponent'

System.IO.FilenotFoundException: Could not load file or assembly 'CefSharp.Core, Version=65.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx' or one of its dependencies. the system cannot find the file specified.

I have the CefSharp.Core added as a reference on both the Usercontrol and the WebFormsApp.

(Note that the control works if it is added programmatically.)

Anyone know what I'm missing?

Edit 1:

Here is the code of the user control that I have created:

using System;
using System.Windows.Forms;
using CefSharp.WinForms;
using CefSharp;

namespace ControlTest
{
    public partial class UserControl1: UserControl
    {
        public ChromiumWebBrowser browser;
        public UserControl1()
        {
            InitializeComponent();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
            CefSettings settings = new CefSettings();
            Cef.Initialize();
            browser = new ChromiumWebBrowser("www.google.com");
            this.panel1.Controls.Add(browser);
        }
    }
}

Solution

  • Finally, thanks to amaitland, I figured out how to make it work. I only needed to avoid creating the component on design time, so I checked if I am in design time:

    if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
        InitializeComponent();
    

    This way the component is only created when in runtime.