是否可以在 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/