How C # cefsharp is used in WinForms applications

Source: Internet
Author: User

Recently done a very small function, open the application on the Web page, with the debug debugging of VS, can open the application normally, can be arranged to IIS above but can not run the application, I Baidu, said is the IIS permissions problem, I do it, how can not toss. Finally boss gave two kinds of scheme, first, discard B/s To change C/s, second, with Cefsharp b/s website embedded in. b/S website has been finished, discard, I used the cefsharp.

Here are the steps to use Cefsharp:

1. Create a basic WinForms application and add Cefsharp to use the NuGet package.

before you create, make sure that the computer is installed: Cefsharp 45.0 and later requires the installation of VC redistributable Package x86, the earlier version requires VC-Redistributable-x86. If it is not installed, the following error is reported.

  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.

Usually install the latest version of Cefsharp, it is recommended to completely turn off vs and then reopen (this will ensure your references are displayed and have full IntelliSense), otherwise you may get an error: The Type or namespace name "Cefsharp" is not found (Are you missing a using directive or assembly reference?) )

2 Changing the platform configuration (x86,x64 or ANYCPU)

I use the cefsharp version is more than 51, so to modify the configuration:

First, search for the your-project-name.csproj file and add it at the first <PropertyGroup> node:

<CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>

Then modify the app. Config file:

<runtime>

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">         <probing privatePath="x86"/>     </assemblyBinding>  </runtime> 3 Cefsharp has been installed and configured to complete, now you can write code.   This is Winfrom code.
usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingCefsharp;usingcefsharp.winforms;usingSystem.Configuration;namespaceVR. winfrom{ Public Partial classForm1:form { PublicChromiumwebbrowser Chromebrowser;  PublicForm1 () {InitializeComponent ();  This. WindowState =formwindowstate.maximized;            Initializechromium (); //Register An object in JavaScript named "Cefcustomobject" with function of the Cefcustomobject class:3Chromebrowser.registerjsobject ("formprocess",NewFormprocess (Chromebrowser, This)); }         Public voidInitializechromium () {cefsettings settings=Newcefsettings (); //Initialize CEF with the provided settingscef.initialize (settings); //Create a browser component            stringURL = configurationmanager.appsettings["URL"]; Chromebrowser=Newchromiumwebbrowser (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 the local resources in the browserBrowsersettings browsersettings =Newbrowsersettings (); Browsersettings.fileaccessfromfileurls=cefstate.enabled; Browsersettings.universalaccessfromfileurls=cefstate.enabled; Browsersettings.websecurity=cefstate.enabled; Chromebrowser.browsersettings=browsersettings; }        Private voidForm1_formclosing (Objectsender, FormClosingEventArgs e)        {Cef.shutdown (); }    }}
This is the code that executes the application
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 being 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);
}


}
}

Last Call to Web page

class="btn btn-primary" onclick="formprocess.opencmd (' c:// Program Files (x86)//google//chrome//application//chrome.exe '); ">Open</button>

Reference URL: http://www.libs.org.cn/index.php?m=content&c=index&a=show&catid=90&id=129

    

How C # cefsharp is used in WinForms applications

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.