Working with Custom Protocols in CefSharp

Опубликовано: 08 Февраль 2018
на канале: Embedded Browsers
3,238
16

An ultra-quick guide on how to working with custom protocols in CefSharp.

About CefSharp: https://cefsharp.github.io/

Transcript:

00:05 First of all we need to create a WPF project with an x86 or x64 configuration and reference the CefSharp library via NuGet. You can find detailed instructions in our previous tutorials.

00:15 Our Next Step is the MainWindow Markup.

00:19 Add the DockPanel and set the LastChildFill property value to ‘true’.

00:25 Add the StackPanel with horizontal orientation to the DockPanel and set its ‘DockPanel.Dock’ property value to ‘Top’.

00:36 Inside the StackPanel, create a TextBox named ‘AddressBox’ and set its width.

00:52 Next to the TextBox, create a button, set its name and text and create its ‘Click’ event handler.

01:08 Finally, add the ChromiumWebBrowser to the DockPanel, set its name and the initial address.

01:18 Implement the Behaviour

01:24 Create the ‘SchemeHandlerFactory’ class, inherit it from the ‘ISchemeFactory’ interface and generate the method stubs. This class is responsible for creating a resource handler for a specified request protocol. The protocol is also named scheme.

01:40 Go to the ‘Create’ method of this class.
Check if the ‘schemeName’ argument equals ‘customscheme’.
If so, return the new instance of the ‘ResourceHandler’ class created using the ‘ResourceHandler.FromString’ method. Pass the request URL as the method argument. In this case the browser should display this URL.
Otherwise, return the new instance of the default ResourceHandler class.

02:10 Now go to the MainWindow constructor.
Before the ‘InitializeComponent’ method call, create a new instance of the ‘CefSettings’ class.

02:21 Call the ‘RegisterScheme’ method of the settings instance. Pass the new instance of the ‘CefCustomScheme’ class as its argument. Inside the instance initializer, set the ‘SchemeName’ property value as the ‘customscheme’ and the ‘SchemeHandlerFactory’ property value as the new instance of the defined class.

02:49 After the settings are created, initialize the ‘Cef’ library using these settings.
Finally, Go to the button click handler and set the ‘Browser.Address’ property value from the ‘AddressBox.Text’ property.

03:07 Build and Launch Your Application
As you can see, the http protocol of the initial URL is handled as usual - the browser instance loads the specified website. But if we specify the registered protocol, the registered handler is called and the browser displays the requested URL.

#CefSharp
#CustomProtocol
#CSS3
#Webpage
#DotNetBrowser
#EmbeddedBrowser