VS2017下把CefSharp编译成AnyCpu、x86、x64模式

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;
        }
    }

参考:https://github.com/cefsharp/CefSharp/issues/1714

发布了85 篇原创文章 · 获赞 31 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/5653325/article/details/85012066