This week, we're going to talk about working witn Context Menus in CefSharp.
You can get the source code of this sample in the GitHub repository:
https://github.com/cefsharptutorials/...
Previous tutorials:
CefSharp Basics: Display an HTML5/CSS3 Webpage - • CefSharp Basics: Display an HTML5/CSS3 Web...
URL Navigation Using CefSharp - • URL Navigation Using CefSharp
Executing JavaScript from the .NET Side Using CefSharp - • Executing JavaScript from the .NET Side Us...
Injecting a .NET Object into JavaScript Using СefSharp - • Injecting a .NET Object into JavaScript Us...
Remote Debugging Using СefSharp - • Remote Debugging Using СefSharp
Getting or Setting HTML Content in CefSharp - • Getting or Setting HTML Content in CefSharp
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:17 Add the CefSharp XML namespace to the MainWindow, then add the ChromiumWebBrowser to the root grid, and set its name and initial address.
00:29 Now Let`s Implement the MainWindow Behaviour.
00:35 Create a new class named SearchContextMenuHandler and inherit it from the IContextMenuHandler interface.
00:44 Create stubs for the interface methods. There are four methods but we really need to implement only two of them.
Go to the OnBeforeContextMenu method.
Clear the menu model.
Then add a new item to the model.
00:56 Bind this menu item to the Find command and set its caption.
01:00 Now we need to define whether this menu item is enabled. It will be enabled if any text in the frame is selected.
01:06 Call the model.SetEnabledAt method, pass the menu item index and check if there is any selected text.
01:12 Now go to the OnContextMenuCommand method. Here we need to perform the required actions if the defined command is executed.
01:19 If the command ID corresponds to the Find command, we need to generate the URL which will be opened after the command execution.
01:26 Let’s use the Bing search engine for the search. The selected text will be the query argument for this service.
01:36 Then execute the JavaScript which will open a new browser window with the generated URL as the initial address.
01:46 Finally, return the true value.
If the command ID does not correspond to the Find command, return the false value.
The return value of this method tells us if the command was handled by the handler or not.
01:58 Clear the content of the OnContextMenuDismissed method and return false from the RunContextMenu method.
02:04 The last step is setting the SearchContextMenuHandler instance as the browser’s menu handler. After the InitializeComponent method call inside the MainWindow constructor, set the BrowserView.MenuHandler property value to a new instance of the SearchContextMenuHandler class.
02:20 So, Now You Can Build and Launch Your Application
#EmbeddedBrowsers
#ContextMenus
#CefSharp