CefSharp從51版本以后開始支持AnyCpu編譯模式
第一步:App.config的configuration下增加一個配置:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="x86"/>
</assemblyBinding>
</runtime>
第二步:編輯項目的csproj文件(可以用記事本,也可以先右鍵卸載項目,然后再右鍵選編輯項目)
在csproj文件的PropertyGroup節點下第一行增加一個配置項
<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>
第三步:Program.cs的內容如下:
static class Program { /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { AppDomain.CurrentDomain.AssemblyResolve += Resolver; LoadApp(); } [MethodImpl(MethodImplOptions.NoInlining)] private static void LoadApp() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); CefSettings settings = new CefSettings { Locale = "zh-CN", BrowserSubprocessPath = @"x86\CefSharp.BrowserSubprocess.exe" }; Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null); Application.Run(new Login()); } // Will attempt to load missing assembly from either x86 or x64 subdir 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; } }
注意: 配置管理器里面的還是要設置平台為x86(為了讓cefsharp.common可以編譯過去), 然后右鍵項目屬性, 在生成標簽頁設置目標平台為AnyCPU, 首選32位