Skip to content

Frequently asked questions

Per Lundberg edited this page Oct 10, 2013 · 10 revisions

1. How do you call Javascript methods from .NET?

Like this (taken from this class):

private void OnWebViewTestExecuteScriptActivated(object sender, EventArgs e)
{
    var script = String.Format("document.body.style.background = '{0}'",
                               colors[color_index++]);
    if (color_index >= colors.Length)
    {
        color_index = 0;
    }

    webView.ExecuteScript(script);
}

If you need to evaluate code which returns a value, use the EvaluateScript() method instead. Note that only trivial values can be returned that way (like int, bool, string etc) - not a complex (user-defined) type which you have defined yourself. This is because there is no (easy) way to expose a random Javascript object to the .NET world, at least not today.

2. How do you expose a .NET class to Javascript?

Like this:

CEF.RegisterJsObject("bound", new BoundObject());

This will make this object available to all WebView instances in your application. If you instead want to make it available to only one specific view, use the WebView.RegisterJsObject() method instead.

3. Why do I get an error about "Could not load file or assembly 'CefSharp.dll' or one of its dependencies. The specified module could not be found." when trying to run my CefSharp-based application? It compiles successfully, but does not run.

This is a common error which is caused by not all files being present in the output directory (bin\Debug or bin\Release, depending on which configuration you are running in Visual Studio). CefSharp.dll is a .NET-based DLL which is dependent on other .dll files, which in turn depends further on other .dll and other files...

Here is a listing of the minimum required files:

  • libcef.dll (The core Chromium DLL, which basically contains the web browser + the CEF embeddability interface which we depend on)
  • icudt.dll (Unicode DLL for Chromium, must also be present)
  • CefSharp.dll (managed .NET assembly which contains the core CefSharp functionality)
  • CefSharp.WinForms.dll OR CefSharp.WPF.dll (depending on your project type)
  • VC2012 Redistributable Package (for running CefSharp on non developer machines)

You then need to initialize CEF with "PackLoadingDisabled = true", using code similar to the following:

var settings = new CefSharp.Settings
{
    PackLoadingDisabled = true,
};

if (CEF.Initialize(settings))
{
    // TODO: Set up your WebView here.
}

This means that the Chrome Developer Tools will be unavailable, which is normally OK for a production-mode application but not quite what you want when developing (web) applications. To make the Chrome dev tools work, add the following files also to the output:

  • devtools_resources.pak
  • locales (directory. Must contain en-US.pak at minimum)

The following files are optional and are used for audio, video, and WebGL:

  • avcodec-54.dll
  • avformat-54.dll
  • avutil-51.dll
  • libEGL.dll
  • libGLESv2.dll

4. Why does the Visual Studio WPF designer not work when I add a WebView to my app?

This is unfortunate, and a consequence of the fact that CefSharp is written in C++ code which in turn references CEF DLL:s. That scenario isn't so well-supported by the Visual Studio designer. This forum thread explains it a bit further and also gives a possible workaround. I think (unfortunately) that the easiest workaround is to just accept the fact that it's not easy to support and just live with it, but there are also other approaches if you really feel you need it.

Clone this wiki locally