C# CefSharp如何在Winforms应用程序中使用
最近做了一个很小的功能,在网页上面打开应用程序,用vs的debug调试,可以正常打开应用程序,可布置到iis上面却无法运行应用程序,吾百度之,说是iis权限问题,吾依理做之,可怎么折腾也不行。最后boss给了两种方案,第一,弃b/s改c/s,第二,用CefSharp把b/s网站嵌进去。b/s网站已做完,弃之可惜,吾便用了CefSharp。
以下是使用CefSharp的步骤:
1.创建一个基本的Winforms应用程序,并添加CefSharp使用NuGet包。
在创建之前,请确保计算机已安装:CefSharp 45.0及更高版本需要安装VC 2013 Redistributable Package x86,早期版本需要VC 2012 Redistributable Package x86。如果未安装,会报以下错误。
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in browser.exe Additional information: Could not load file or assembly 'CefSharp.Core.dll' or one of its dependencies.
通常安装最新版本的CefSharp,建议完全关闭VS,然后重新打开(这样可以确保您的引用显示,并有完整的intellisense),否则你可能会发生错误:找不到类型或命名空间名称“Cefsharp”(是否缺少using指令或程序集引用?)
2 更改平台配置(x86,x64或AnyCPU)
吾用的CefSharp版本是51以上,所以要修改配置:
首先,搜索your-project-name.csproj文件,并在第一个 <PropertyGroup>的节点添加:
<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>
然后修改app.config文件:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="x86"/> </assemblyBinding> </runtime>
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using System.Configuration; namespace VR.Winfrom
{
public partial class Form1 : Form
{
public ChromiumWebBrowser chromeBrowser; public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
InitializeChromium();
// Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3
chromeBrowser.RegisterJsObject("formProcess", new FormProcess(chromeBrowser, this));
} public void InitializeChromium()
{
CefSettings settings = new CefSettings();
// Initialize cef with the provided settings
Cef.Initialize(settings);
// Create a browser component
string url = ConfigurationManager.AppSettings["Url"];
chromeBrowser = new ChromiumWebBrowser(url); // 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 browser
BrowserSettings browserSettings = new BrowserSettings();
browserSettings.FileAccessFromFileUrls = CefState.Enabled;
browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
browserSettings.WebSecurity = CefState.Enabled;
chromeBrowser.BrowserSettings = browserSettings;
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Cef.Shutdown();
}
}
}

这是执行应用程序的代码

using System.Diagnostics;
using CefSharp.WinForms;
using VR.DAL;
using System; namespace VR.Winfrom
{
public class FormProcess
{
// Declare a local instance of chromium and the main form in order to execute things from here in the main thread
private static ChromiumWebBrowser _instanceBrowser = null;
// The form class needs to be changed according to yours
private static Form1 _instanceMainForm = null;
public FormProcess(ChromiumWebBrowser originalBrowser, Form1 mainForm)
{
_instanceBrowser = originalBrowser;
_instanceMainForm = mainForm;
} public void opencmd(string filePath)
{
string file = @"" + filePath;
ProcessStartInfo start = new ProcessStartInfo(file);
Process.Start(start);
}
}
}

最后web页面的调用
<button class="btn btn-primary" onclick="FormProcess.opencmd('C://Program Files (x86)//Google//Chrome//Application//chrome.exe');">Open</button>
参考网址:http://www.libs.org.cn/index.php?m=content&c=index&a=show&catid=90&id=129
出处:https://www.cnblogs.com/shimiyan/p/6932073.html
C# CefSharp如何在Winforms应用程序中使用的更多相关文章
- 转:如何在32位程序中突破地址空间4G的限制
//如何在32位程序中突破地址空间4G的限制 //首先要获得内存中锁定页的权限 #define _WIN32_WINNT 0x0501 //xp系统 #include <windows.h> ...
- 如何在Android应用程序中使用传感器模拟器SensorSimulator
原文地址; 如何在Android应用程序中使用传感器模拟器 - 移动平台应用软件开发技术 - 博客频道 - CSDN.NET http://blog.csdn.net/pku_android/arti ...
- [zz]如何在C语言程序中处理汉字
学习过C语言的人也许有时会遇到这样一个问题:如何用变量存储汉字以及对这些变量进行操作.目前许多C语言参考书中都没涉及到这个问题,程序中多为处理英文变量和英文字符串,涉及到汉字的情况也大都是在print ...
- 如何在Silverlight应用程序中获取ASP.NET页面参数
asp.net Silverlight应用程序中获取载体aspx页面参数 有时候SL应用中需要使用由aspx页面中传递过来的参数值,此时通常有两种方法获取 1. 使用InitParameters属性, ...
- 如何在WPF应用程序中使用视频处理控件TVideoGrabber
要在WPF 中使用 TVideoGrabber 组件,需要像下面的方法来使用 VS.NET(DLL) 版本的组件: ——复制TVideoGrabber_x.x.x.x_x86.dll到c:/windo ...
- 如何在Android应用程序中使用传感器(OpenIntents开源组织SensorSimulator项目)
原文地址http://blog.sina.com.cn/s/blog_621c16b101013ygl.html OpenIntents项目和可用资源介绍 [1]. 项目介绍:OpenInten ...
- [Prism框架实用分享]如何在Prism应用程序中使用日志
前言 在Prism中有关日志的命名空间: Microsoft.Practices.Prism.Logging 在Prism中,你可以使用Prism自带的Logger(比如TextLogger等),也可 ...
- 示例 - 如何在Console应用程序中应用SpiderStudio生成的DLL?
以前面生成的XML/JSON互转DLL为例, 我们写一个Console Appliction来做这件事情, 步骤如下: 1. 创建Console Project 2. 引入www.utilities_ ...
- 如何在Qt 4程序中优化布局结构(表格讲解,很清楚)
原文地址:http://blog.csdn.net/qter_wd007/archive/2010/03/13/5377882.aspx 在迄今为止讲到每一个例子中,我们只是简单的把窗口部件放置到某个 ...
随机推荐
- iOS史上最简单修改导航栏分隔线颜色方法!!!
override func viewDidLoad() { super.viewDidLoad() if let imageView = self.findNavLineView(view: navi ...
- MacbookPro15 2019 闪屏雪花现象方案汇总
1. 系统偏好设置,显示器,关闭 "自动调节亮度" "原彩显示",即取消勾选. 2. 系统偏好设置,节能,关闭 "自动切换图形卡模式",即取 ...
- redis启动警告解决
vim /etc/rc.localecho never > /sys/kernel/mm/transparent_hugepage/enabled加入上面那句到/etc/rc.local,开机启 ...
- EF的 NoTracking 的一些记录
NoTracking官方解释 跟踪与非跟踪查询 跟踪行为可控制 Entity Framework Core 是否将有关实体实例的信息保留在其更改跟踪器中. 如果已跟踪某个实体,则该实体中检测到的任何更 ...
- 【ARM-Linux开发】Linux模块机制浅析
Linux模块机制浅析 Linux允许用户通过插入模块,实现干预内核的目的.一直以来,对linux的模块机制都不够清晰,因此本文对内核模块的加载机制进行简单地分析. 模块的Hello World! ...
- 第N个丑数
#include <bits/stdc++.h> using namespace std; #define ll long long /* 把只包含质因子2.3和5的数称作丑数(Ugly ...
- scrollview的优化
针对一次加载很多格子的scrollview的优化,第一次只加载可视区域的格子,其他的用空物体占位,在每次滑动时检测需要实例化的格子,通过对象池重用第一次的格子.可以根据每行格子的数量只检测每行的第一个 ...
- lua【卤鹅】总结
转自:https://www.cnblogs.com/reblue520/p/10767428.html 编写一个简单的hello world程序 test.lua 如果觉得简单,可以给一个for循环 ...
- spring mvc 处理pojo传递对象时该对象继承父类的属性在网络接收端接收该属性值总是null,why?
//=========================== 情形一: ===============================//在网络上传递User1类对象时info属性值在网络的另一端能够接 ...
- 【C++】继承和组合的概念?什么时候用继承?什么时候用组合?
继承:通过扩展已有的类来获得新功能的代码重用方法 组合:新类由现有类的对象合并而成的类的构造方式 何时用继承?何时用组合? 1.如果二者间存在一个"是"的关系,并且一个类要对另外一 ...