gpt4 book ai didi

c# - 如何让 CefSharp 使用 vs 公共(public)库中的配置 AnyCPU

转载 作者:行者123 更新时间:2023-11-30 22:59:13 25 4
gpt4 key购买 nike

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

<Prefer32Bit>true</Prefer32Bit>

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

最佳答案

请参阅文档:General Usage Guide

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

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

然后,添加<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>到第一个PropertyGroup.csproj包含 CefSharp.Wpf 的文件PackageReference CefSharp.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;
}

最后,至少使用这些设置初始化 CefSharp:

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

关于c# - 如何让 CefSharp 使用 vs 公共(public)库中的配置 AnyCPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52394485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com