右键单击带有WPF的CefSharp Chromium webbrowser时,删除默认上下文菜单

时间:2016-11-09 13:59:28

标签: wpf xamarin wpf-controls cefsharp

在我的WPF应用程序中,我在CefSharp Webbrowser中加载了一个URL。当我从鼠标右键单击时,默认上下文菜单显示如下图像

enter image description here

我的xaml代码是

<Window x:Class="StoneApp.WebviewWindow" WindowState="Maximized"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" 
    xmlns:local="clr-namespace:StoneApp"
    mc:Ignorable="d"
    Title="MyApp">

<DockPanel>

    <Grid x:Name="mygrid">
        <cefSharp:ChromiumWebBrowser x:Name="my_webview" IsBrowserInitializedChanged ="InitializeBrowser" Grid.Row="0" MouseRightButtonDown="Rightbtn_MouseDown"/>
    </Grid>

</DockPanel>

</Window>

我的xaml.cs代码是

  public WebviewWindow(string webresponse)
    {
        InitializeComponent();
        this.webresponse = webresponse;
    }

    private void InitializeBrowser(object sender, DependencyPropertyChangedEventArgs e)
    {
        my_webview.Load(webresponse);
    }

    private void Rightbtn_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.RightButton == MouseButtonState.Pressed)
        {
            e.Handled = false;
        }
    }

当我点击右键时,我无法拨打&#39; Rightbtn_MouseDown&#39;功能也是。所以我的问题是当我右键单击webbrowser视图时,我想删除/隐藏默认的上下文菜单。

1 个答案:

答案 0 :(得分:1)

您只需覆盖默认的IContextMenuHandler

webView.MenuHandler = new MenuHandler();

internal class MenuHandler : IContextMenuHandler
{
    public bool IContextMenuHandler.RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
    {
        return false;
    }

    public void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
    {
        model.Clear();
    }
}

您可以在此处详细了解:https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf.Example/Handlers/MenuHandler.cs