Winform下CefSharp的引用、配置、实例与报错排除(源码)
Winform 下 CefSharp 的引用, 配置, 实例与报错排除
[TOC]
1, 关于 CefSharp
装一手, 比较简单的英语Itisa lightweight.NET wrapper around theChromiumEmbeddedFramework(CEF)byMarshallA.Greenblatt.About30%of the bindings are writteninC++/CLIwiththe majority of code hereisC#. It can be used from C# or VB, or any other CLR language. CefSharp provides both WPF and WinForms web browser control implementations.
CefSharpisBSD licensed,so it can be usedinboth proprietaryandfree/open source applications.Forthe full details,see the LICENSE file.
自己总结的:
CefShar 是一个提供了 Chromium Embedded Framework (CEF) 的. NET 接口的开源项目, 提供了 Winform,WPF 封装, 可以用来代替微软的 WebBrowser, 浏览网页, 尤为强大的是实现了 C#,VB 等 vs 支持的语言与 JavaScript 的交互方法.
2,CefSharp 项目源码下载
源码下载 目前最新 v53.0.1 https://github.com/cefsharp/CefSharp
源码结构介绍 http://www.h3399.cn/201805/577286.html
3,Winfrom 项目引入 CefSharp
我的开发环境 vs2013,.net4.0,Nugget 中搜索 CefSharp 显示的版本是 v53, 然而
v51.0.0-pre01
的 Breaking Changes 里面有这么一句
不想安装. net4.5.2, 所以只能通过
工具 - 程序包管理器 - 程序包管理控制台
手动命令行导入版本 v49.0.1 的, 输入命令
Install-Package CefSharp.WinForms -Version 49.0.1
回车等待执行完成, 我这里已经安装过了.
其他版本命令请参考.
等待导入成功, 生成一下... 报错...
嗯?!!!
如果你以为只是在
项目名右击 - 属性 - 生成 - 目标平台
改为 x86 就太天真了.... 反正我试了不行... 正确的姿 zhi 势 shi:
解决方案名右击 - 属性 - 配置属性 - 配置
, 右边平台选择 x86 或 x64, 什么? 选不到?
点击当前界面右上角
配置管理器 - 活动解决方案平台
下拉
新建
,x86/x64 随便选, 回来这边下拉已经可以选择了, 完事左下角确定, 再生成一下试试吧
以上看不懂的参考这里
4,Winfrom 下 CefSharp 的基本使用
4.1 显示一个页面
4.1.1 显示 url 网页
对照下面在一个 Form 的对应位置添加代码usingCefSharp.WinForms;
publicpartialclassForm1:Form
{
ChromiumWebBrowserwebBrower=null;
publicForm1()
{
InitializeComponent();
Load+=Form_Load;
}
privatevoidForm_Load(objectsender,EventArgse)
{
stringpath="www.baidu.com";
webBrower=newChromiumWebBrowser(path);
webBrower.Dock=DockStyle.Fill;// 填充方式
this.Controls.Add(webBrower);
}
}
直接运行就 OK 了
4.1.2 显示一个本地 html
把 path 改为 File 协议就行, 例如: 显示程序下的文件夹 html 中的 test.html// 获取文件的物理路径
stringpath=AppDomain.CurrentDomain.BaseDirectory+"\\html\\test.html";
// 转换为 File 协议路径
path="file://"