如何将上下文菜单添加到 WebBrowser wp7? [英] how to add context menu to WebBrowser wp7?

查看:28
本文介绍了如何将上下文菜单添加到 WebBrowser wp7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to add Context Menu to WebBrowser in WP7?(like IE)

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

silverlight toolkit Context Menu not support WebBrowser!!!

推荐答案

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

The WebBrowser does not support context menus and doesn't function like other Silverlight controls. Therefore, it's not possible to directly add a ContextMenu. However, there are workarounds possible. One of them is to use the InvokeScript method. You should read this thread. Apparently the code at the bottom of the thread works. Note: GINternet is the WebBrowser control's name, so you'll need to change that to yours.

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
      {
      }
}

这篇关于如何将上下文菜单添加到 WebBrowser wp7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆