【问题标题】:how to add context menu to WebBrowser wp7?如何将上下文菜单添加到 WebBrowser wp7?
【发布时间】:2011-12-25 21:46:15
【问题描述】:

是否可以在 WP7 中为 WebBrowser 添加上下文菜单?(如 IE)

silverlight 工具包上下文菜单不支持 WebBrowser!!!

【问题讨论】:

    标签: c# silverlight windows-phone-7 silverlight-toolkit


    【解决方案1】:

    WebBrowser 不支持上下文菜单,并且与其他 Silverlight 控件的功能不同。因此,无法直接添加 ContextMenu。但是,有一些可能的解决方法。其中之一是使用 InvokeScript 方法。你应该阅读this thread。显然线程底部的代码有效。 注意:GINternet 是 WebBrowser 控件的名称,因此您需要将其更改为您的。

    public void AttachContextMenu()
    {
          try
          {
               if (GINternet.IsScriptEnabled)
               {
                    GINternet.InvokeScript("execScript", "function FindParentLink(item) \r\n{\r\n\tif (!item.parentNode)\r\n\t\treturn null;\r\n\tif (item.tagName.toLowerCase() == 'a') \r\n\t{\r\n\t\treturn item;\r\n\t} \r\n\telse \r\n\t{\r\n\t\treturn FindParentLink(item.parentNode);\r\n\t}\r\n}\r\n\r\nfunction FindParentImage(item) \r\n{\r\n\tif (!item.parentNode)\r\n\t\treturn null;\r\n\tif (item.tagName.toLowerCase() == 'img') \r\n\t{\r\n\t\treturn item;\r\n\t} \r\n\telse \r\n\t{\r\n\t\treturn FindParentImage(item.parentNode);\r\n\t}\r\n}\r\n\r\nfunction HandleContextMenu() \r\n{\r\n\tvar linkItem = FindParentLink(event.srcElement);\r\n    var imageItem = FindParentImage(event.srcElement);\r\n    var notifyOutput = '';\r\n    if (linkItem != null) if (linkItem.href != null) notifyOutput += linkItem.href;\r\n    if (imageItem != null) if (imageItem.src != null) notifyOutput += imageItem.src;\r\n    if (notifyOutput != '')\r\n        window.external.notify(notifyOutput);\r\n    else\r\n\t\twindow.external.notify('NOTLINKIMG');\r\n}");
                    GINternet.InvokeScript("execScript", "document.oncontextmenu = HandleContextMenu;");
               }
          }
          catch
          {
          }
    }
    

    【讨论】:

    • 非常非常感谢但是我有一个问题如何在我的点击位置下显示上下文菜单我使用这个:<phone:WebBrowser Name="webBrowser1"> <toolkit:ContextMenuService.ContextMenu> <toolkit:ContextMenu x:Name="menu1"> <toolkit:MenuItem Header="item1" /> </toolkit:ContextMenu> </toolkit:ContextMenuService.ContextMenu> </phone:WebBrowser> 并且上下文菜单显示在页面顶部!!!
    • @sma6871 你能详细说明你是如何让它工作的吗?我有一个显示单个图像的网络浏览器控件。如果用户将手指放在上面,我想显示一个显示“保存图像”的上下文菜单。
    • @zyash 只是将keyboardP 答案的函数复制到您的代码中,并在您的页面加载时调用它。现在,当用户将手指放在 webbrowser 控件上时,ScriptNotify 将被触发。图片的链接在e.value中。
    猜你喜欢
    • 1970-01-01
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多