CefSharp.WinForms 应用程序同时支持32位(x86)和64位(x64)的解决方案

当我们为基于 .NET Framework 的 WinForm 程序增加 CefSharp.WinForms 依赖后,可能会遇到以下报错信息

1
2
3
CefSharp.Common is unable to proceeed as your current Platform is ‘AnyCPU’. To target AnyCPU please read https://github.com/cefsharp/CefSharp/issues/1714. Alternatively change your Platform to x86 or x64 and the relevant files will be copied automatically. For details on changing your projects Platform see https://docs.microsoft.com/en-gb/visualstudio/ide/how-to-configure-projects-to-target-platforms?view=vs-2017 CefSharpDemo

CefSharp.Common.targets

修改.csproj文件。

1
2
3
4
<PropertyGroup>
<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport> <!--添加-->
...
</PropertyGroup>

修改App.config文件。

1
2
3
4
5
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="x86"/>
</assemblyBinding>
</runtime>

测试

1
2
3
4
string url = "https://www.baidu.com";
chromeBrowser = new ChromiumWebBrowser(url);// Add it to the form and fill it to the form window.
this.Controls.Add(chromeBrowser);
chromeBrowser.Dock = DockStyle.Fill;