Cefsharp_ceph nfs

Cefsharp_ceph nfs文章目录0.简介1.安装2.H.264支持3.加载本地HTML文件4.多个窗口显示浏览器0.简介CefSharp,简单来说就是一款.Net编写的浏览器包,方便你在Winform和WPF中内嵌的Chrome浏览器组件。它支持HTML5。CefSharp的功能比较复杂,以下只介绍一些我觉得比较重要的并且目前经常用的功能,此“挑食”之意也。1.安装CefSharp的安装过程如下:打开VisualStduio,新建一个Windows窗体应用(.NETFramework);在“工具”

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

0. 简介

CefSharp,简单来说就是一款.Net编写的浏览器包,方便你在Winform和WPF中内嵌的Chrome浏览器组件。它支持HTML5。
CefSharp的功能比较复杂,以下只介绍一些我觉得比较重要的并且目前经常用的功能。

1. 安装

CefSharp的安装过程如下:

  1. 打开Visual Stduio,新建一个Windows窗体应用(.NET Framework)
  2. 在“工具”菜单打开NuGet包管理器;
  3. 搜索“CefSharp.WinForms”进行安装;
  4. CefSharp不能在“Any CPU”平台上运行,需要配置。打开“配置管理器”;
    在这里插入图片描述
  5. 新建“x86”和“x64”两个平台。从理论上来说使用x86或者x64平台都行,但由于之后要使用编译好的支持h264的x86内核,因此此处选择x86平台;
    在这里插入图片描述
  6. 在 Form1.cs 中添加如下代码;
using CefSharp;
using CefSharp.WinForms;
using System;
using System.Windows.Forms;

namespace WindowsFormsApp8 { 
   
    public partial class Form1 : Form { 
   
        ChromiumWebBrowser browser;
        public void InitBrowser() { 
   
            CefSettings settings = new CefSettings();

            // Note that if you get an error or a white screen, you may be doing something wrong !
            // Try to load a local file that you're sure that exists and give the complete path instead to test
            // for example, replace page with a direct path instead :
            // String page = @"C:\Users\SDkCarlos\Desktop\afolder\index.html";

            // String page = string.Format(@"{0}\html-resources\html\index.html", Application.StartupPath);
            String url = "http://www.html5test.com";

            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            browser = new ChromiumWebBrowser(url);

            // Add it to the form and fill it to the form window.
            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;

            // Allow the use of local resources in the browser
            BrowserSettings browserSettings = new BrowserSettings();
            browserSettings.FileAccessFromFileUrls = CefState.Enabled;
            browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
            browser.BrowserSettings = browserSettings;
        }

        public Form1() { 
   
            InitializeComponent();
            InitBrowser();
        }
    }
}
  1. 调试程序,发现窗口有白边,原因是没有设置高分屏适配,按照C# WinForm程序设计的第1.3节的方法设置即可;
  2. 调试程序,得到最终效果。
    在这里插入图片描述

2. H.264支持

默认情况下,CefSharp是不支持H.264的,因此不可以进行视频播放。为了使其支持视频播放,需要修改其内核文件,操作步骤如下:
注:请确保安装的CefSharp.WinForms是79.1.360版本的!

  1. 下载我的老师(感谢他!)编译好的支持H264的内核文件 libcef.dll ;
    链接:下载地址
    提取码:8q2u
  2. 用其替换packages目录下的同名文件;
    在这里插入图片描述
  3. 调试程序,查看效果。显然浏览器已支持H.264。
    在这里插入图片描述

3. 加载本地HTML文件

除了使用CefSharp浏览器访问网络地址,还有一个重要的用途就是访问本地文件。
首先将需要用到的HTML和其他静态文件拷贝到工程目录,并设置“复制到输出目录”:
在这里插入图片描述
然后编写如下代码:

using CefSharp;
using CefSharp.WinForms;
using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp8 { 
   
    public partial class Form1 : Form { 
   
        ChromiumWebBrowser browser;
        public void InitBrowser() { 
   
            CefSettings settings = new CefSettings();

            // Note that if you get an error or a white screen, you may be doing something wrong !
            // Try to load a local file that you're sure that exists and give the complete path instead to test
            // for example, replace page with a direct path instead :
            // String page = @"C:\Users\SDkCarlos\Desktop\afolder\index.html";

            // String page = string.Format(@"{0}\html-resources\html\index.html", Application.StartupPath);
            String url = string.Format(@"{0}\ui\test.html", Application.StartupPath);

            if (!File.Exists(url)) { 
   
                MessageBox.Show("Error: File doesn't exists : " + url);
            }

            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            browser = new ChromiumWebBrowser(url);

            // Add it to the form and fill it to the form window.
            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;

            // Allow the use of local resources in the browser
            BrowserSettings browserSettings = new BrowserSettings();
            browserSettings.FileAccessFromFileUrls = CefState.Enabled;
            browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
            browser.BrowserSettings = browserSettings;
        }

        public Form1() { 
   
            InitializeComponent();
            InitBrowser();
        }
    }
}

调试程序,可以看到效果:
在这里插入图片描述

4. 多个窗口显示浏览器

如果有多个窗口都需要显示浏览器,每个窗口的代码并不完全相同。
主窗口的代码如上一节所示,新的窗口的代码如下:

using System;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using System.IO;

namespace WindowsFormsApp8 { 
   
    public partial class Form2 : Form { 
   
        ChromiumWebBrowser browser;
        public void InitBrowser() { 
   
            String url = string.Format(@"{0}\ui\test.html", Application.StartupPath);

            if (!File.Exists(url)) { 
   
                MessageBox.Show("Error: File doesn't exists : " + url);
            }

            browser = new ChromiumWebBrowser(url);

            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
        }

        public Form2() { 
   
            InitializeComponent();
            InitBrowser();
        }
    }
}

显然,与主窗口的区别是去掉了“Cef.Initialize()”部分。
运行效果如下:
在这里插入图片描述

5. 执行JavaScript代码

如果需要通过程序控制网页的显示、流程,最明显的方法莫过于执行JavaScript代码了。特别是如果使用Vue.js框架,那么通过JS可以实现“模板渲染”的功能。以下展示了一个示例。
注意:ExecuteScriptAsync()方法是异步执行的,程序并不会阻塞!

  1. 编辑HTML文件(Vue.js框架),增加函数接口;
<script> const vue = new Vue({ 
     el: '#app', data: { 
     title: '标题1', }, }); function changeTitle(title) { 
     vue.title = title; } </script>
  1. 编写C#代码,请注意两处中文注释的部分;
using CefSharp;
using CefSharp.WinForms;
using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp8 { 
   
    public partial class Form1 : Form { 
   
        ChromiumWebBrowser browser;
        public void InitBrowser() { 
   
            CefSettings settings = new CefSettings();

            // Note that if you get an error or a white screen, you may be doing something wrong !
            // Try to load a local file that you're sure that exists and give the complete path instead to test
            // for example, replace page with a direct path instead :
            // String page = @"C:\Users\SDkCarlos\Desktop\afolder\index.html";

            // String page = string.Format(@"{0}\html-resources\html\index.html", Application.StartupPath);
            String url = string.Format(@"{0}\ui\test.html", Application.StartupPath);

            if (!File.Exists(url)) { 
   
                MessageBox.Show("Error: File doesn't exists : " + url);
            }

            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            browser = new ChromiumWebBrowser(url);

            // Add it to the form and fill it to the form window.
            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;

            // Allow the use of local resources in the browser
            BrowserSettings browserSettings = new BrowserSettings();
            browserSettings.FileAccessFromFileUrls = CefState.Enabled;
            browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
            browser.BrowserSettings = browserSettings;

            // 当页面加载完毕时,执行JavaScript代码
            browser.ExecuteScriptAsyncWhenPageLoaded("changeTitle('我不是鸭鸭')");
        }

        public Form1() { 
   
            InitializeComponent();
            InitBrowser();
        }

        private void button1_Click(object sender, EventArgs e) { 
   
            // 当点击按钮时,执行JavaScript代码
            browser.ExecuteScriptAsync("changeTitle('我是鸭鸭')");
        }
    }
}
  1. 调试程序,查看效果。
    在这里插入图片描述
    在这里插入图片描述

如果需要执行JavaScript代码并得到返回值,代码可以这么编写:
注意:EvaluateScriptAsync()方法是同步执行的,也就是程序会阻塞!

private void button1_Click(object sender, EventArgs e) { 
   
        // 当点击按钮时,执行JavaScript代码
        Task<JavascriptResponse> response = browser.EvaluateScriptAsync("add(1, 2)");

        // response.Result.Result是Object类型
        MessageBox.Show("返回值为:" + response.Result.Result);
}

JavaScript代码:

function add(a, b) { 
   
    for (var i = 0; i < 9999999999; ++i) { 
   
        ;
    }
    return a + b;
}

6. 在JS中调用C#方法

在C#中编写如下代码:(注意中文注释的部分)
以下是同步执行的演示,即JS会阻塞等待C#执行完再继续执行。

using CefSharp;
using CefSharp.WinForms;
using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp8 { 
   
    public partial class Form1 : Form { 
   
        ChromiumWebBrowser browser;

        // 创建一个类,用于在JS中访问
        public class JsEvent { 
   
            public void SayHello() { 
   
                MessageBox.Show("你好,C#!");
            }
        }

        public void InitBrowser() { 
   
            CefSettings settings = new CefSettings();

            // Note that if you get an error or a white screen, you may be doing something wrong !
            // Try to load a local file that you're sure that exists and give the complete path instead to test
            // for example, replace page with a direct path instead :
            // String page = @"C:\Users\SDkCarlos\Desktop\afolder\index.html";

            // String page = string.Format(@"{0}\html-resources\html\index.html", Application.StartupPath);
            String url = string.Format(@"{0}\ui\test.html", Application.StartupPath);

            if (!File.Exists(url)) { 
   
                MessageBox.Show("Error: File doesn't exists : " + url);
            }

            // Initialize cef with the provided settings
            Cef.Initialize(settings);
            // Create a browser component
            browser = new ChromiumWebBrowser(url);

            // Add it to the form and fill it to the form window.
            this.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;

            // Allow the use of local resources in the browser
            BrowserSettings browserSettings = new BrowserSettings();
            browserSettings.FileAccessFromFileUrls = CefState.Enabled;
            browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
            browser.BrowserSettings = browserSettings;

            // 绑定JavaScript对象
            CefSharpSettings.LegacyJavascriptBindingEnabled = true;
            CefSharpSettings.WcfEnabled = true;
            BindingOptions bindingOptions = new BindingOptions();
            // 如果不加此句,那么绑定的方法不能以大写字母开头
            bindingOptions.CamelCaseJavascriptNames = false;
            browser.JavascriptObjectRepository.Register("jsObj", new JsEvent(), isAsync: false, options: bindingOptions);
        }

        public Form1() { 
   
            InitializeComponent();
            InitBrowser();
        }
    }
}

在HTML中编写如下代码:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="./index.css" rel="stylesheet">
    <script src="./vue.min.js"></script>
    <script src="./index.js"></script>
</head>
<body>

<div id="app">
    <el-card class="box-card">
        <div slot="header" class="clearfix">
            <span>{
  
  { title }}</span>
            <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
        </div>
        <div v-for="o in 4" :key="o" class="text item">
            {
  
  { '列表内容 ' + o }}
        </div>
    </el-card>
    <el-button @click="onclick">触发</el-button>
</div>

<script> const vue = new Vue({ 
     el: '#app', data: { 
     title: '标题1', }, methods: { 
     onclick: function () { 
     jsObj.sayHello(); console.log('调用完毕!'); } }, }); </script>

</body>
</html>

运行效果:
在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191701.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • VsCode系列(一):下载安装及汉化「建议收藏」

    VsCode系列(一):下载安装及汉化「建议收藏」VsCode的下载和安装在这篇文章中将为大家介绍WIN10操作系统如何下载安装并且汉化VsCode的方法。VsCode入门VisualStudioCode是一个轻量级但功能强大的源代码编辑器

    2022年8月4日
    6
  • 占位图怎么写[通俗易懂]

    占位图怎么写[通俗易懂]想必大家对占位图都不会陌生吧,非常犀利的一个工具,当然也有非常多优秀的网站为我们提供这样的接口。唯一遗憾的是国内的站点非常少。当然不是说国外的不行,正好相反,国外的那些占位图非常人性化,非常方便,唯一

    2022年8月4日
    6
  • eplan激活码分享【最新永久激活】

    (eplan激活码分享)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html70YZDJVTFP-eyJsaWNlbnNlSW…

    2022年3月26日
    3.7K
  • 什么叫侧面指纹识别_侧面指纹VS屏下指纹和后置指纹,原来我们都低估了侧面指纹…「建议收藏」

    什么叫侧面指纹识别_侧面指纹VS屏下指纹和后置指纹,原来我们都低估了侧面指纹…「建议收藏」原标题:侧面指纹VS屏下指纹和后置指纹,原来我们都低估了侧面指纹随着科技的发展,手机花式解锁方式也是让人眼花缭乱。最早的指纹解锁就是密码和手势解锁,后来为了增加屏幕屏占比,还采用了屏下指纹和前后置指纹解锁,以及侧面指纹解锁。这么多指纹解锁方式你最喜欢哪个呢?屏下解锁2018年1月,vivo发布全球首款屏下指纹解锁手机,vivoX20UD屏幕指纹版。指纹解锁速度在0.6S左右,与传统指纹识别0…

    2022年8月10日
    6
  • 个人博客数据库设计

    个人博客数据库设计文章目录前言一、概述及分析1.1项目背景1.2分析1.3系统功能1.3.1用户管理1.3.2博文管理1.3.3评论管理1.3.4分类管理1.3.5标签管理二、数据库概念模型设计—基本ER图三、数据库逻辑模型设计四、数据库语句4.1建立数据表语句(部分)4.2建立视图(部分)五、小结&amp;参考资料小结参考资料前言最近要做数据库大作业,在思考了很久之后,还是设计一个简单…

    2022年6月20日
    32
  • Python 股票历史数据的获取

    Python 股票历史数据的获取本文主要讨论的是pytho免费股票数据的获取及处理。国内提供股票数据的接口如sinajs,money.163.com,yahoo,它们提供的API接口不同,每家提供的数据大同小异,可以选择一家的数据来处理。

    2022年6月24日
    43

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号