Context menu

This article describes how to work with the context menu.

By default, the context menu is suppressed for the IBrowser instance. If an IBrowserView instance is initialized from the IBrowser instance that does not have custom context menu handling, the default context menu handler is registered. The items shown in the default context menu depend on the web page item that was right-clicked, however, in most cases it contains the following items: “Back”, “Forward”, “Reload”, “Print” and “View page source”.

The default context menu implementation cannot be modified. If you need to add or remove some items, you should create and register your own implementation of the context menu handler.

Custom context menu

To build a custom context menu, the IBrowser.ShowContextMenuHandler handler should be used. Here is an example:

browser.ShowContextMenuHandler = 
    new AsyncHandler<ShowContextMenuParameters, ShowContextMenuResponse>(ShowMenu);
private Task<ShowContextMenuResponse> ShowContextMenu(ShowContextMenuParameters parameters)
{
    TaskCompletionSource<ShowContextMenuResponse> tcs = 
        new TaskCompletionSource<ShowContextMenuResponse>();
            WebView.Dispatcher?.BeginInvoke(new Action(() =>
            {
                System.Windows.Controls.ContextMenu popupMenu = 
                    new System.Windows.Controls.ContextMenu();

                if (!string.IsNullOrEmpty(parameters.LinkText))
                {
                    popupMenu.Items.Add(BuildMenuItem("Show the URL link", 
                                                        true, 
                                                        Visibility.Visible, 
                                                        delegate
                    {
                        string linkURL = parameters.LinkUrl;
                        Console.WriteLine($"linkURL = {linkURL}");
                        MessageBox.Show(linkURL, "URL");
                        tcs.TrySetResult(ShowContextMenuResponse.Close());
                    }));
                }

                popupMenu.Items.Add(BuildMenuItem("Reload", 
                                                    true, 
                                                    Visibility.Visible, 
                                                    delegate
                {
                    Console.WriteLine("Reload current web page");
                    browser.Navigation.Reload();
                    tcs.TrySetResult(ShowContextMenuResponse.Close());
                }));
                popupMenu.Closed += (sender, args) => 
                { 
                    tcs.TrySetResult(ShowContextMenuResponse.Close()); 
                };
                popupMenu.IsOpen = true;
            }));
            return tcs.Task;
}

The complete example is available in our repository: C#, VB.NET

Disable context menu

To supress the context menu completely, the following code should be used:

browser.ShowContextMenuHandler =
     new Handler<ShowContextMenuParameters, ShowContextMenuResponse>(p =>
    {
        return ShowContextMenuResponse.Close();
    });
Go Top