WinForm CefSharp 笔记一(入门篇)
简介
CefSharp简单来说就是一款.Net编写的浏览器包,方便你在Winform和WPF中内嵌的Chrome浏览器组件。
资源
GitHub地址:传送门
wiki帮助文档地址:传送门
CefSharp最小的示例工程:传送门
gitter交流讨论区:传送门
快速入门
本文使用版本cefsharp/71
要求
- Visual Studio with NuGet Package Manager (>=2010).
- CefSharp45.0 和更新的版本,需要安装 VC 2013 Redistributable Package x86
- 更早的版本需要安装VC 2012 Redistributable Package x86
- .Net Framework4.5.2
安装
这块安装使用没有想象的那么简单,比较坑爹,各种修改配置,按照官网的A配置方案没有搞定,按照B配置方案勉强部署成功(VS2013/VS2017)!对于外文不好的我,看着英文文档脑壳疼。老外给的闭坑指南,但是感觉没有啥卵用。下面就介绍一下B方案安装部署的过程吧,A方案我就不讲了,想看的请去上面的官网查看。
简略测试部署过程
整个工程可在GitHub下载:传送门
- 创建工程Test.App(Winform工程),将其中的Form1窗体删掉。
- 创建工程Test.Chrome(类库)。
- 在Test.Chrome工程添加NuGet引用,搜索CefSharp,选择CefSharp.Winforms。
- 在解决方案上点配置管理器,将平台设置为x86或x64.
- 在Test.Chrome工程添加Form1窗体,添加CefSharp窗体相关的代码。
- Test.App添加Test.Chrome工程的引用,修改Program.cs文件,引用Test.Chrome工程的Form1窗体。
部署过程细节截图
- 创建一个基础的Winform应用,并使用NuGet引用CefSharp包。使用Nuget添加引用,搜索
CefSharp
,添加CefSharp.WinForm
,CefSharp.Winform依赖好几个包,这块选择这一个安装就可以了,NuGet会自动帮你把其他依赖的包一并下载好的。
安装完你本地的Packages文件夹里有如下文件:
官方文档建议
:安装完NuGet包之后,关闭vs然后重新打开,避免VS自带的智能感知引用有问题
- 在简介方案上右键—》选择配置管理—》修改目标平台为x86或x64
选择x86或x64
- 在你的窗体Form1窗体里添加相应代码,参考Using CEF (as Browser)中的代码。
Using CEF (as Browser)
在代码中引用相应的dll
using CefSharp;
using CefSharp.WinForms;
完整示例:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;namespace embebbedChromium
{public partial class Form1 : Form{public ChromiumWebBrowser chromeBrowser;public Form1(){InitializeComponent();// Start the browser after initialize global componentInitializeChromium();}private void Form1_Load(object sender, EventArgs e){}//初始化浏览器并启动public void InitializeChromium(){CefSettings settings = new CefSettings();// Initialize cef with the provided settingsCef.Initialize(settings);// Create a browser componentchromeBrowser = new ChromiumWebBrowser("https://www.baidu.com");// Add it to the form and fill it to the form window.this.Controls.Add(chromeBrowser);chromeBrowser.Dock = DockStyle.Fill;}//窗体关闭时,记得停止浏览器private void Form1_FormClosing(object sender, FormClosingEventArgs e){Cef.Shutdown();} }
}
3.2. Using CEF (as User Interface)
这块参考官方文档:传送门,做了部分内容的完善,这块我理解的就是提供了一个js调用C#类方法的一个示例。
下载Bootstrap相关的文件,传送门
将下载好的Bootstrap文件夹复制拷贝到你的VS项目中,并添加html文件夹,在里面新建一个
index.html
文件,具体如下图所示:
html文件内容参考这里:传送门,网页下方有示例,也有模板可以下载。<!DOCTYPE html> <html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --><title>Bootstrap 101 Template</title><!-- Bootstrap --><link href="css/bootstrap.min.css" rel="stylesheet"><!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --><!-- WARNING: Respond.js doesn't work if you view the page via file:// --><!--[if lt IE 9]><script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script><script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]--></head><body><h1>Hello, world!</h1><!-- jQuery (necessary for Bootstrap's JavaScript plugins) --><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script><!-- Include all compiled plugins (below), or include individual files as needed --><script src="js/bootstrap.min.js"></script></body> </html>
将上面的css、fonts、html、js文件夹里的文件
全部选中
—》然后点击鼠标右键,选中属性
—》设置始终复制
新建一个类CefCustomObject,用来让js调用C#中类的方法,具体代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; using System.Diagnostics;namespace embebbedChromium {class CefCustomObject{// Declare a local instance of chromium and the main form in order to execute things from here in the main threadprivate static ChromiumWebBrowser _instanceBrowser = null;// The form class needs to be changed according to yoursprivate static Form1 _instanceMainForm = null;public CefCustomObject(ChromiumWebBrowser originalBrowser, Form1 mainForm){_instanceBrowser = originalBrowser;_instanceMainForm = mainForm;}public void showDevTools(){_instanceBrowser.ShowDevTools();}public void opencmd(){ProcessStartInfo start = new ProcessStartInfo("cmd.exe", "/c pause");Process.Start(start);}} }
修改Form1窗体的代码
using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms; using System.Runtime.InteropServices;namespace embebbedChromium {public partial class Form1 : Form{public ChromiumWebBrowser chromeBrowser;public Form1(){InitializeComponent();// Start the browser after initialize global componentInitializeChromium();//需要添加此句代码,否则下面执行会报错CefSharpSettings.LegacyJavascriptBindingEnabled = true; // Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3chromeBrowser.RegisterJsObject("cefCustomObject", new CefCustomObject(chromeBrowser, this));}private void Form1_Load(object sender, EventArgs e){//此句代码执行有错,官网示例的跑不起来//chromeBrowser.ShowDevTools();}public void InitializeChromium(){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 page = @"C:\Users\SDkCarlos\Desktop\artyom-HOMEPAGE\index.html";if (!File.Exists(page)){MessageBox.Show("Error The html file doesn't exists : "+page);}// Initialize cef with the provided settingsCef.Initialize(settings);// Create a browser componentchromeBrowser = new ChromiumWebBrowser(page);// Add it to the form and fill it to the form window.this.Controls.Add(chromeBrowser);chromeBrowser.Dock = DockStyle.Fill;// Allow the use of local resources in the browserBrowserSettings browserSettings = new BrowserSettings();browserSettings.FileAccessFromFileUrls = CefState.Enabled;browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;chromeBrowser.BrowserSettings = browserSettings;}private void Form1_FormClosing(object sender, FormClosingEventArgs e){Cef.Shutdown();}} }
- 运行查看效果,如下图所示
- 运行查看效果,如下图所示
WinForm CefSharp 笔记一(入门篇)相关推荐
- JavaScript学习笔记之入门篇
JavaScript学习笔记之入门篇 JavaScript引入 1. 页面级 js: 2. 外部js文件: JavaScript变量 1. 变量的作用: 2. 声明变量: 3. 变量赋值: 4. 单一 ...
- javaSE学习笔记01 入门篇
javaSE学习笔记01 入门篇 java语言概述 Java背景知识 java是 美国 sun 公司 在1995年推出的一门计算机高级编程语言. java早期称为Oak(橡树),后期改名为Java. ...
- WinForm CefSharp 笔记二
在上一篇博文<WinForm内置浏览器之CefSharp 笔记一>,简单介绍了CefSharp的快速入门.这篇博文接着上篇,主要记录C#如果调用JS中的方法. 本文主要参照Github示例 ...
- Java学习笔记系列-入门篇-计算机基础
Java学习笔记 Java学习笔记是一个持续更新的系列,工作多年,抽个空对自身知识做一个梳理和总结归纳,温故而知新,同时也希望能帮助到更多正在学习Java 的同学们. 本系列目录: 入门篇 计算机基础 ...
- 经典再现,看到就是赚到。尚硅谷雷神 - SpringBoot 2.x 学习笔记 - 基础入门篇
SpringBoot 2.x 时代 – 基础入门篇 视频学习地址:https://www.bilibili.com/video/BV1Et411Y7tQ?p=112&spm_id_from=p ...
- Calcite-学习笔记(入门篇)
目录 参考文章 什么是Apache Calcite? 为什么需要Apache Calcite? 什么是关系代数Relational algebra ? 什么是数据库系统的查询优化? Calcite架构 ...
- Swi-Prolog学习笔记(入门篇)
首先介绍一个学习swi-prolog 和amzi-prolog很靠谱的中文网站:http://hyry.dip.jp/tech/book/page/prolog/search_game_maxmin. ...
- 持续集成学习笔记-入门篇(1)持续集成基本概念
今年7月份中下旬,笔者见过一个号称"资深开发者"的哥们(据说编程有十来年了),笔者问他:"你们平时用的持续集成工具都有哪些?"这哥们回答:"那些都是骗 ...
- 金融常识学习笔记之入门篇
金融常识学习笔记 拉弗曲线 住房痛苦指数 CPI 什么是CPI? 如何才能跑赢CPI? 工资上涨能跑赢CPI吗? PPI 什么是PPI? PPI有什么作用? 恩格尔系数 什么是恩格尔系数? 恩格尔系数 ...
- UVM学习笔记—快速入门篇
UVM指的是验证方法学,是学习数字验证的入门课程.它是至关重要的,有不少人往IC验证方向发展的,多多少少都会去了解UVM.但UVM并不是简单的翻个书就可以学会的,还是要掌握学习方法或者跟着老师学习的. ...
最新文章
- biopython有什么用_BioPython学习笔记
- android studio import android-source-code
- 弹性架构_实践中的弹性基础架构
- 百度否认退市;微信官方回应「个人影响度报告」;微软公布 C# 9.0 计划 | 极客头条...
- Go语言的基准测试简单示例
- ImportError: cannot import name UnrewindableBodyError
- 新版php卡盟排行榜网站源码
- word取消限制编辑
- 炉石传说的代码是Java吗,hearthstone: 炉石传说,JAVA模拟器(HearthStone Simulator for Java)...
- [渝粤教育] 西南科技大学 电子测量与仪表 在线考试复习资料
- 视频教程-微信小程序快速入门视频课程-微信开发
- 数学有什么用处?看完后恍然大悟!
- 解决方案——笔记本HDMI外接2k显示器如何调2k分辨率
- 大数据系统开发综合实践(一)
- B. Pleasant Pairs
- 自家的智能家居方案研究
- W3school html 表格用法总结
- 【文献阅读】能兼顾图像理解和推理能力的VQA模型(CVPR,2019)
- Debug的使用方法(转)
- 前端开发工程师面试题集锦(一)