C#:在AnyCPU模式下使用CefSharp

摘要:
添加段落<3.添加一端<应用程序下。配置;asm.v1“>应用程序{publicApp(){//AddCustomassemblyresolverAppDomain.CurrentDomain.AssemblyResolve+=解析器;
本篇博客讲述如何在AnyCPU模式下使用CefSharp

因为在某些情况下,不得不用AnyCPU,但是CefSharp支持的是86和64位俩种模式,所以在我查阅了很多国内外的资料下,总结出来的一些精华

参考地址:

https://ourcodeworld.com/articles/read/173/how-to-use-cefsharp-chromium-embedded-framework-csharp-in-a-winforms-application

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

https://github.com/cefsharp/CefSharp.MinimalExample/blob/demo/anycpu/CefSharp.MinimalExample.Wpf/App.xaml.cs

三篇结合就可以实现在AnyCPU下使用CefSharp

简单步骤记录

第一篇博客取其第二个,更改配置的那一块,这块不改,下面的没用,项目起不起来

  简述一下:

  1.修改为首选32位,

    C#:在AnyCPU模式下使用CefSharp第1张

  2.在你项目名.csproj文件下,加一段

<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>

    位置如下:

    C#:在AnyCPU模式下使用CefSharp第2张

  3.在App.config下加一端

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

    位置如下:

    C#:在AnyCPU模式下使用CefSharp第3张

第二篇博客取的精髓在这块,我给放过来

复制代码
public partial class App : Application
{
    public App()
    {
        //Add Custom assembly resolver
        AppDomain.CurrentDomain.AssemblyResolve += Resolver;

        //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)]
    private static void InitializeCefSharp()
    {
        var settings = new CefSettings();

        // Set BrowserSubProcessPath based on app bitness at runtime
        settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                                               Environment.Is64BitProcess ? "x64" : "x86",
                                               "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
    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;
    }
}
复制代码

第三篇博客 就是告诉你using指令怎么引,以及详细的写法,要用的话,还是用第二篇博客的这段代码

复制代码
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;

namespace CefSharp.MinimalExample.Wpf
{
    public partial class App : Application
    {
        public App()
        {
            //Add Custom assembly resolver
            AppDomain.CurrentDomain.AssemblyResolve += Resolver;

            //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)]
        private static void InitializeCefSharp()
        {
            //Perform dependency check to make sure all relevant resources are in our output directory.
            var settings = new CefSettings();
            settings.BrowserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                                                   Environment.Is64BitProcess ? "x64" : "x86",
                                                   "CefSharp.BrowserSubprocess.exe");

            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
        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;
        }
    }
}
复制代码
三套组合拳打完,CefSharp就可以在AnyCPU模式下使用了

 转自:https://www.cnblogs.com/DawnCHENXI/p/9288947.html

免责声明:文章转载自《C#:在AnyCPU模式下使用CefSharp》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇经典算法_数组解决IE中部分文件格式不能下载的问题(附MIME大全)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF DispatcherTimer(定时器应用) 无人触摸60s自动关闭窗口

如果无人触摸:60s自动关闭窗口 xmal:部分 <s:SurfaceWindow x:Class="SurfaceApplication1.SurfaceWindow1"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://s...

WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu

一.前言   申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接。   本文主要内容: 菜单Menu的自定义样式; 右键菜单ContextMenu的自定义样式; 树控件TreeView的自定义样式,及右键菜单实现。 二.菜单Me...

bpc 数据处理

UJKT程序:UJO_SQE_TESTBADI:UJ_CUSTOM_LOGIC*XDIM_MEMBERSET Z_ACCOUNT = PL04 *START_BADI PL04_STEP1 WRITE = ON QUERY = ON *END_BADI *定义模型 types:begin of ty_planning, m...

nuxt基础一

nuxt项目的文件夹目录 1.nuxt-link组件和router-link组件一样 2.pages文件夹中文件名就是路由路径名(无需配置路由路径),是页面组件。   laouts文件夹,是公共的模板的内容组件,公共的组件<Nuxt />,必须要加 不过在 Nuxt.js 框架中,我们有了新的变化,layouts对应目录中的layouts文件...

Beetl使用注意事项

1.如果表达式跟定界符或者占位符有冲突,可以在用 “” 符号 @for(user in users){ email is ${user.name}@163.com @} ${[1,2,3]} //输出一个json列表 ${ {key:1,value:2 } } //输出一个json map,} 需要加上 2.Beetl里定义的临时变量类型默认对应的ja...

uni-app中使用scroll-view滚到底部时多次触发scrolltolower

一、前言、scroll-view基本属性: 前言:   前段时间使用scroll-view可滚动视图区域容器来做多个不同内容的展示(在我这个页面中同时使用了三个scroll-view做数据展示),因为这几个展示的内容的数据都比较的多,因此为了页面的数据加载顺畅决定使用上拉加载(简单的说就是数据分页显示)。 页面组成如下图所示: scroll-view属性...