Getting started
An overview of CefSharp, how to use it, and an overview of some of the included features.
An overview of CefSharp, how to use it, and an overview of some of the included features.
Welcome to CefSharp! We are excited about you being here.
So, what is CefSharp, really? You probably have a bit of a clue already, since you've actually managed to download (and perhaps even compile) the software. Nonetheless, let's try to explain it in a few bullet points:
Perhaps very obvious, but still worth mentioning. Since CefSharp is based on CEF, which is in turn based on Chromium (version 33 for the time being), CefSharp provides one of the best HTML5 rendering experiences available - 470+ points out of 555 on html5test.com. Note: YMMV depending on .DLLs you add and features you enable.
Chromium 33 supports both x86 and x64, and so do we as of CefSharp 33.0.0 For the time being, it does not "auto-detect" the platform being used (since this is quite complex), so you have to choose either one for your project. This practically means that you will have to compile & package separate binaries/installers of your app for x86 and x64 respectively if you want/need to support both of them.
Declare a factory class like this:
internal class CefSharpSchemeHandlerFactory : ISchemeHandlerFactory { // This is the name of your custom scheme. public const string SchemeName = "custom"; public ISchemeHandler Create() { return new CefSharpSchemeHandler(); } }...and the actual scheme handler class like this:
internal class CefSharpSchemeHandler : ISchemeHandler { public bool ProcessRequestAsync(IRequest request, SchemeHandlerResponse response, OnRequestCompletedHandler requestCompletedCallback) { // TODO: Add your custom scheme code here, so that "bytes" is a byte[] with the // TODO: response data for the given request. Examine the "request" object for info // TODO: about the URI being requested and so forth. response.ResponseStream = new MemoryStream(bytes); response.MimeType = GetMimeType(fileName); // This signals to CefSharp that your scheme handler is ready with the processing. requestCompletedCallback(); // You can also return "false" here if the requested URI is invalid for your scheme // handler. return true; } }Finally, you have to register this scheme handler using some code like this:
public static void Init() { // Pseudo code; you probably need more in your CefSettings also. var settings = new CefSettings(); settings.RegisterScheme(new CefCustomScheme { SchemeName = CefSharpSchemeHandlerFactory.SchemeName, SchemeHandlerFactory = new CefSharpSchemeHandlerFactory() }); Cef.Initialize(settings); }It's important that the scheme registration takes place before the
Cef.Initialize()
gets
called. (This is different to how things was being done in CefSharp version 1.)
CefSharp supports custom schemes using an
asynchronous pattern, which is desirable if
your custom scheme handler performs e.g. network requests or other long-running operations. In other, more
trivial cases (e.g. if you are just serving out static content which is already loaded in memory), you can
just signal using the requestCompletedCallback()
straight away, as in the example above.
If you are reading this page in either one of the CefSharp.Wpf.Example or CefSharp.WinForms.Example sample applications, you can use the boxes on the right side of the screen to run arbitrary JavaScript code towards the context of this page. By default, the content of the block below will be modified/inspected by the script code.
You can modify the value of this text field using JavaScript!The C# code for performing these kinds of interactions is quite simple. Like this:
webBrowser.ExecuteScriptAsync(someScriptCode);
The code above will run the provided JavaScript snippet (which may do interesting things, like interrogating or modifying the DOM of the page, just to name one example out of many potential ones). The execution is of the "fire-and-forget" style; any result of the execution is silently disregarded. The execution is also asynchronous in nature, a term which means that (among other things) the method may return before the actual code has actually been executed.
This is the preferrably approach if possible, since it does not deadlock the UI in any way. However, we realize that it's not suitable for all scenarios. Have faith — there is a solution even for cases where you do need to return a value. Just write your code like this:
var result = webBrowser.EvaluateScript("10 + 20");
Please note that only a limited number of data types are supported when returning the result above. Simple value types (int, float, etc) and strings all work, but do not expect to be able to return other JavaScript objects.
Here are a few pointers that might help you further explore the wonderful world of CefSharp: