如何在Web浏览器中添加上下文菜单?

时间:2013-05-03 02:43:43

标签: c# windows-phone-7 windows-phone-8

我知道已有一个与此相关的主题,但我需要更多信息。目前,我有这个代码,我从另一个线程,

    public void AttachContextMenu()
    {
        try
        {
            if (webTab1.IsScriptEnabled)
            {
                webTab1.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}");
                webTab1.InvokeScript("execScript", "document.oncontextmenu = HandleContextMenu;");
            }
        }
        catch
        {
        }
    }

线程说当浏览器加载完毕后你只需要调用这个方法,虽然它似乎不起作用。

关于如何让它发挥作用的任何想法?

1 个答案:

答案 0 :(得分:0)

仅适用于Windows Phone 7.X。不是8。

AddHandler wb.Loaded, Sub(sendr As Object, ev As EventArgs)
    wb.IsScriptEnabled = True
    AttachContextMenu()
End Sub
AddHandler wb.ScriptNotify, Sub(sender As Object, e As Microsoft.Phone.Controls.NotifyEventArgs)
    Diagnostics.Debug.WriteLine(e.Value.ToString)
End Sub

如果您知道如何在WP8 WebBrowser控件中打开上下文菜单,请让我们都知道。 在WP8上,我使用onmousedown事件,一个计时器和onmouseout事件与WP7中的findparentlink函数结合使用。

相关问题