c# - 如何将上下文菜单添加到 WebBrowser wp7?

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

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

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

最佳答案

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

关于c# - 如何将上下文菜单添加到 WebBrowser wp7?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8631746/

相关文章:

c# - 为什么 DataTemplate.LoadContent() 不遵守模板定义的触发器?

css - Windows 7 移动版 CSS 媒体查询

windows-phone-7 - Windows Phone 上的 Azure ACS token 续订

windows-phone-7 - UI有很多元素时如何快速导航?

c# - 我需要 WPF 的 OCR

c# - 从网络引用下载 $metadata 时出错

c# - Entity Framework - 如何将复杂对象添加到数据库

javascript - JavaScript 线程和 Silverlight UI 线程之间是什么关系?

silverlight - 在 silverlight 中使用 mvvm 进行异步调用

Silverlight ScrollViewer 初始水平偏移