Working with Plugins in CefSharp

Опубликовано: 11 Январь 2018
на канале: Embedded Browsers
7,847
39

Learn how to working with plugins in CefSharp.

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

Previous tutorials:
CefSharp Basics: Display an HTML5/CSS3 Webpage:    • CefSharp Basics: Display an HTML5/CSS...  

URL Navigation Using CefSharp:    • URL Navigation Using CefSharp  

Executing JavaScript from the .NET Side Using CefSharp:    • Executing JavaScript from the .NET Si...  

Injecting a .NET Object into JavaScript Using СefSharp:    • Injecting a .NET Object into JavaScri...  

Remote Debugging Using СefSharp:    • Remote Debugging Using СefSharp  

Getting or Setting HTML Content in CefSharp:    • Getting or Setting HTML Content in Ce...  

Working with Context Menus in CefSharp:    • Working with Context Menus in CefSharp  

Transcript:
00:06 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:17 Our Next Step is the MainWindow Markup.
Create two column definitions in the root grid.

00:33 Add a stack panel with vertical orientation to the root grid and place it in the left column.

00:44 Add a stack panel with horizontal orientation to the outer stack panel.

00:52 Add a checkbox named ‘ViewPDF’ to the inner stack panel. This checkbox will be used for changing the state of the PDF Viewer plugin.

01:07 Add a button named ‘Refresh’ to the inner stack panel and create a ‘Click’ event handler.

01:25 Add a list box named ‘PluginsListBox’ to the outer stack panel. This list box will be used to display the list of the available plugins.

01:35 Finally add the DockPanel, set its name and set the LastChildFill property to True. This panel will contain the ChromiumWebBrowser component.

01:51 Implement the Behaviour. Create the ‘RequestContextHandler’ class, inherited from the ‘IRequestContextHandler’ interface and generate the method stubs.

02:08 Add the bool field named ‘DisplayPdf’.

02:17 Go to the GetCookieManager method. This method is not necessary for us right now, so just return the null value.

02:23 Go to the OnBeforePluginLoad method. This method defines whether the specified plugin should be loaded or disabled.

02:33 Check if the current plugin name contains 'PDF'. If yes, set the 'pluginPolicy' value to 'Allow' if the 'DisplayPdf' value is 'true'. Otherwise, set it to 'Disable'. This method should return true if we need to use the specified plugin policy and false for using the default plugin policy.

03:02 Go to the MainWindow class. Define the 'browserView' field of the 'ChromiumWebBrowser' type.

03:11 Define the 'InitializeBrowser' method and go inside it.

03:15 Every time we change the plugin policies we need to reinitialize the browser, so first of all we need to dispose of the old browser.

03:37 Then create the 'contextHandler' variable.

03:48 Set its 'DisplayPdf' property value accordingly to the 'ViewPDF' checkbox state.

04:06 Create the new browser. Set its RequestContext property value to the new instance of the RequestContext class. Create such an instance using the previously created contextHandler.

04:20 Load the sample pdf file to the browser and add the browser to the dock panel.

04:33 This method should be called after the InitializeComponent method in the MainWindow constructor.

04:39 Now let’s display the list of the available plugins. Create the async 'DisplayPlugins' method.

04:49 Inside this method get the plugins list using the async static 'GetPlugins' method of the 'Cef' class.

05:01 Then pass the names of the plugins to the list box items source.

05:15 Call this method at the end of the MainWindow constructor.

05:21 Finally, call the InitializeBrowser method from the 'Refresh' button click handler. Also let's check the 'ViewPDF' checkbox by default.

05:43 Build and launch
You can see that by default the PDF file is displayed. Also, the list of the available plugins is in the left side of the window.
If we uncheck the checkbox and press the Refresh button, CefSharp is reloaded and the pdf file is not displayed.

#EmbeddedBrowsers
#WorkingwithPlugins
#CefSharp