我已经创建了一个带有WPF窗口的类库项目。在一个WPF窗口中,我想获得一个CefSharp浏览器。我的项目应该是配置AnyCPU。在不同的教程中,我看到在带有AnyCPU的可执行项目中调优CefSharp配置的要点之一是设置(csproj)
<Prefer32Bit>true</Prefer32Bit>但是在类库项目中,此属性是禁用的。如何在类库中启用AnyCPU对CefSharp的支持?
发布于 2018-09-19 06:27:12
请参阅文档:一般使用指南
有几种支持AnyCPU的解决方案。我使用了以下方法:
首先,通过NuGet安装依赖项。
然后,将<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>添加到.csproj文件的第一个PropertyGroup中,该文件包含CefSharp.Wpf.ChromiumWebBrowser控件的CefSharp.Wpf PackageReference。
现在,编写一个程序集解析器,根据当前体系结构找到正确的非托管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);https://stackoverflow.com/questions/52394485
复制相似问题