how to add context menu to WebBrowser wp7?

一个人想着一个人 提交于 2020-01-01 18:27:27

问题


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

silverlight toolkit Context Menu not support WebBrowser!!!


回答1:


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


来源:https://stackoverflow.com/questions/8631746/how-to-add-context-menu-to-webbrowser-wp7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!