標籤:win partial csp style system har osi led dock
最近做了一個很小的功能,在網頁上面開啟應用程式,用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>
3 cefsharp已經安裝並配置完成,現在就可以寫代碼了.
這是winfrom的代碼
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
C# CefSharp如何在Winforms應用程式中使用