如何让 CefSharp 使用 vs 公共库中的配置 AnyCPU

use*_*105 4 c# wpf cefsharp

我创建了一个包含 WPF 窗口的类库项目。在一个 WPF 窗口中,我想要一个 CefSharp 浏览器。我的项目应该是配置AnyCPU。在不同的教程中,我看到使用CefSharp在可执行项目中调整AnyCPU配置的要点之一是设置 ( csproj )

<Prefer32Bit>true</Prefer32Bit>
Run Code Online (Sandbox Code Playgroud)

但是在类库项目中,这个属性是禁用的。如何在我的类库中为 CefSharp 启用 AnyCPU 支持?

Fla*_*ver 7

请参阅文档:一般使用指南

有多种解决方案可以启用 AnyCPU 支持。我使用了以下内容:

首先,通过NuGet安装依赖项。

然后,添加<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>到所述第一PropertyGroup所述的.csproj含有文件CefSharp.Wpf PackageReferenceCefSharp.Wpf.ChromiumWebBrowser控制。

现在,编写一个程序集解析器以根据当前架构查找正确的非托管 DLL:

AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;

private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name.StartsWith("CefSharp"))
    {
        string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
        string architectureSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
            Environment.Is64BitProcess ? "x64" : "x86",
            assemblyName);

        return File.Exists(architectureSpecificPath)
            ? Assembly.LoadFile(architectureSpecificPath)
            : null;
    }

    return null;
}
Run Code Online (Sandbox Code Playgroud)

最后,至少使用以下设置初始化 CefSharp:

var settings = new CefSettings()
{
    BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
        Environment.Is64BitProcess ? "x64" : "x86",
        "CefSharp.BrowserSubprocess.exe")
};
Cef.Initialize(settings);
Run Code Online (Sandbox Code Playgroud)