using CefSharp; using CefSharp.Wpf; using System; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Threading; using System.Windows;
// Any CefSharp references have to be in another method with NonInlining // attribute so the assembly rolver has time to do it's thing. InitializeCefSharp();
// 启动主程序 }
[MethodImpl(MethodImplOptions.NoInlining)] privatestaticvoidInitializeCefSharp() { var settings = new CefSettings();
// Set BrowserSubProcessPath based on app bitness at runtime settings.BrowserSubprocessPath = GetCefSharpFilePath("CefSharp.BrowserSubprocess.exe");
// Make sure you set performDependencyCheck false Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null); }
// Will attempt to load missing assembly from either x86 or x64 subdir // Required by CefSharp to load the unmanaged dependencies when running using AnyCPU privatestatic Assembly Resolver(object sender, ResolveEventArgs args) { if (args.Name.StartsWith("CefSharp")) { string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll"; string archSpecificPath = GetCefSharpFilePath(assemblyName); return File.Exists(archSpecificPath) ? Assembly.LoadFile(archSpecificPath) : null; } returnnull; }
默认 CefSharp 仅允许在设置为 x86 或 x64 平台下运行,修改设置即可。 但是有时会在配置了允许 Any CPU 后出现 x86 平台下正常 x64 平台下运行报同样错误,最后找到问题出现在独立创建用的于调用 Cef 库不知何时生成出一些不必要的内容,删除 project.csproj 文件下不必要的内容即可。