System.Windows.Markup.XamlParseException:'CefSharp.Wpf.ChromiumWebBrowser' 抛出异常。内部异常:无法加载 CefSharp.Core.dll

System.Windows.Markup.XamlParseException: 'CefSharp.Wpf.ChromiumWebBrowser' threw exception. Inner Exception: could not load CefSharp.Core.dll

使用 C# XAML Windows 使用 CefSharp 及其 ChromiumWebBrowser 的文档查看器应用程序。该项目构建良好,没有错误,但每当我尝试使用 Chromium open/view 应用程序上的图像时,会弹出一个错误:

System.Windows.Markup.XamlParseException: The invocation of constructor on type CefSharp.Wpf.ChromiumWebBrowser that matches the specified binding constraints threw an exception

Inner Exception: FileNotFoundException: Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies

CefSharp.Common 包已安装在项目中,CefSharp.Wpf 包也是如此,我还添加了对项目的引用。

这是我的 ChromeControl.xaml:

<UserControl x:Class="Viewer.Chrome.ChromeControl">
    <-- namespaces defined here -->
    <Grid>
        <chrome:ChromiumWebBrowser Name="viewer" RenderOptions.BitmapScalingMode="HighQuality" TextOptions.TextRenderingMode="ClearType" />
    </Grid>
</UserControl>

这是我在 Viewer.Chrome 命名空间中的 ChromeControl.xaml.cs:

public partial class ChromeControl : UserControl {
    public ChromeControl(ViewerWindow viewerWindow) {
            InitializeComponent();
            viewer.RegisterJsObject("chrome", new ChromeDomHelper(viewerWindow, viewer));
            viewer.MenuHandler = new ChromeContextMenu();
    }
    public ChromiumWebBrowser Viewer { get { return viewer; } }
}

而且我发现它在我的文档 Viewer.xaml.cs:

中开始的堆栈跟踪中
ChromeControl chrome = new ChromeControl(viewerWindow);

已经搜索了很多方法来解决这个问题,但 none 似乎对我有用,任何帮助将不胜感激。

更新

如果您的项目是针对 .NET Framework 4.5,那么您必须将项目的 .NET Framework 更改为 4.6

  1. 将名称为 Nuget.Config 的文件添加到解决方案(而不是任何项目)
  2. 将以下内容添加到此文件

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
          <packageSources>
              <add key="nuget.org" value="https://nuget.org/api/v2/" />
              <add key="cefsharp-myget" value="https://www.myget.org/F/cefsharp/" />
          </packageSources>
          <activePackageSource>
              <add key="All" value="(Aggregate source)" />
          </activePackageSource>
          <solution>
              <add key="disableSourceControlIntegration" value="true" />
          </solution>
    </configuration>
    

3.try现在。

原答案

我以前遇到过这个异常,我想你需要调用 Initialize 方法,我记得。

using CefSharp;

protected override void OnStartup(StartupEventArgs e)
{
      base.OnStartup(e);
      Cef.Initialize();
}

看到这个FAQ会有帮助。

这里总结一下

  1. Why do I get an error about "Could not load file or assembly 'CefSharp.Core.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? It runs on my developer machine, though throws an exception when I copy it to another computer?

This is a common error, typically one of the following

VC++ 2012/2013 Redistributable Package is required in order to run CefSharp on non developer machines. See FAQ #6 below for more information. You can include the required dlls as part of your application. Not all dependencies are present in the executing folder. CefSharp includes unmanaged dll's and resources, these are copied to the executing folder via two .props file which are included in your project when you install the NuGet packages. See list of required files below, make sure the required files are present. You packaged your application for distribution via an installer and it doesn't run on the target machine. Installers don't include the unmanaged resources by default, you'll need to add them manually. For ClickOnce, see #1314 for some pointers and solutions other users have come up with. A list of required files can be found here: Output files description (Redistribution)

NOTE: This also applies if you get a FileNotFoundException when initializing the WPF control in XAML.

NOTE 2: If compiling from source (not recommended, use the NuGet packages) and you notice that you can no longer build in debug mode, but release builds work just fine you may need to repair your version of Visual Studio. This happens in rare cases where you will get the same exact message as a missing unmanaged .dll file as shown above.

纠结了这么久,终于解决了。相当愚蠢的错误,我在所需的安装路径中丢失了一些文件,这些文件也在 link 中指定:
Output files description table (Redistribution)

我在 SO 上问了另一个关于 WiX 安装程序的问题,基本上在安装路径中有所有文件后,我能够调试它并让它工作。

Link 我的另一个问题 (WiX):
WiX installer project: CefSharp.Core.dll could not be loaded, file not found