WPF中CefSharp的AnyCPU配置(WPF中CefSharp的AnyCPU配置)
WPF中CefSharp的AnyCPU配置(WPF中CefSharp的AnyCPU配置)
之前找了不少资料,下面是自己测试过的步骤
步骤一:添加节点CefSharpAnyCpuSupportunload项目后可以编辑.csproj文件。或者直接用记事本编辑。
每个用到CefSharp的项目都添加。
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
...
<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>
</PropertyGroup>
步骤二:在App.xaml.cs构造函数里添加代码
public App()
{
// 当程序集的解析失败时 从x86或x64子目录加载缺少的程序集
AppDomain.CurrentDomain.AssemblyResolve = Resolver;
// 初始化CefSharp
InitializeCefSharp();
}
/// <summary>
/// CefSharp初始化设置
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private static void InitializeCefSharp()
{
var settings = new CefSettings();
//设置语言
settings.Locale = "zh-CN";
//禁用gpu 防止浏览器闪烁
settings.CefCommandLineArgs.Add("disable-gpu" "1");
//去掉代理,增加加载网页速度
settings.CefCommandLineArgs.Add("proxy-auto-detect" "0");
settings.CefCommandLineArgs.Add("no-proxy-serve" "1");
// 在运行时根据系统类型(32/64位),设置BrowserSubProcessPath
settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase Environment.Is64BitProcess ? "x64" : "x86" "CefSharp.BrowserSubprocess.exe");
// 确保设置performDependencyCheck为false
CefSharp.Cef.Initialize(settings performDependencyCheck: false browserProcessHandler: null);
}
// 尝试从x86或x64子目录加载缺少的程序集
// 在使用AnyCPU运行时,CefSharp要求加载非托管依赖项
private static Assembly Resolver(object sender ResolveEventArgs args)
{
if (args.Name.StartsWith("CefSharp"))
{
string assemblyName = args.Name.Split(new[] { ' ' } 2)[0] ".dll";
string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase Environment.Is64BitProcess ? "x64" : "x86" assemblyName);
return File.Exists(archSpecificPath) ? Assembly.LoadFile(archSpecificPath) : null;
}
return null;
}
其他可选配置说明:修改app.config文件
- 只需要修改启动项目下的app.config文件
- 这段配置的意思是运行时从x86目录加载程序集,需要勾选首选32位,否则编译成64位程序会报错。
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="x86"/>
</assemblyBinding>
</runtime>
</configuration>