将上下文菜单添加到WP8浏览器控件

时间:2013-05-10 11:22:38

标签: c# javascript browser windows-phone-8 contextmenu

当用户在网页上的链接上执行保持手势时,我尝试附加上下文菜单。

我在网上搜索过并找到了一些建议HERE

if (webBrowser.IsScriptEnabled)
        {
            webBrowser.InvokeScript("execScript", "function eventListener(evt){ if (evt.type == 'MSPointerDown') { gestureHandler.addPointer(evt.pointerId); return; } if (evt.detail & evt.MSGESTURE_FLAG_END) {  window.external.notify(evt.srcElement.tagName);}}");
            webBrowser.InvokeScript("execScript","document.addEventListener('MSGestureHold', eventListener, false); document.addEventListener('MSPointerDown', eventListener, false);  gestureHandler = new MSGesture(); gestureHandler.target = document.body;");
        }

但是第二个execScript引发了这个错误

 System.SystemException was unhandled by user code
  HResult=-2146233087
  Message=An unknown error has occurred. Error: 80020101.
  Source=Microsoft.Phone.Interop
  StackTrace:
   at Microsoft.Phone.Controls.NativeMethods.ValidateHResult(Int32 hr)
   at Microsoft.Phone.Controls.WebBrowserInterop.InvokeScript(String scriptName, String[] args)
   at Microsoft.Phone.Controls.WebBrowser.InvokeScript(String scriptName, String[] args)
   at Tabbed_Browser.User_Controls.WebBrowser.AttachContextMenu()
   at Tabbed_Browser.User_Controls.WebBrowser.webBrowser_Loaded(Object sender, RoutedEventArgs e)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
  InnerException: 

我已根据此posting尝试了以下内容。但显然它只适用于WP7手机,而不适用于WP8或模拟器。

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

我通过ScriptNotify监控结果但从未解雇

    private void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
    {
        Debug.WriteLine(e.Value.ToString());
    }

任何人都知道如何在WP8浏览器控件中附加上下文菜单?

修改

我在WebBrowser控件上找到了window.navigator.msPointerEnabled为false的信息,在Internet Explorer应用程序中找到了True。这意味着我们无法在控件中正确实现触摸事件检测。我们可以将其设置为启用吗?

4 个答案:

答案 0 :(得分:0)

如果window.navigator.msPointerEnabled为false,则可以使用onmousedown和onmouseup进行监控

答案 1 :(得分:0)

试图让WP8网页浏览器控件做一些技巧后,这里有几个观察。

......

System.SystemException
消息=发生了未知错误。错误:80020101。

...通常意味着您的javascript未正确解析。通常是语法错误。

我发现删除CR,LF和TAB字符会降低噪音,并使VisualStudio JavaScript编辑器窗口中的语法错误更容易一些。 String.Replace()是你的朋友。

我只是成功使用InvokeScript()来执行匿名函数。这种模式已经成功:(注意标点符号)

webBrowser.InvokeScript("eval", "(function (param1) { window.external.notify(param1); })('this is your message');");

在您的具体情况下,我看到定义了三个函数但导致它们被执行的原因是什么?也许这解释了例外。

位于Gyle

答案 2 :(得分:0)

这是我的解决方案。它适用于Windows Phone 8

<phone:WebBrowser
                x:Name="WebBrowser"
                IsHitTestVisible="True"
                IsScriptEnabled="True"
                LoadCompleted="WebBrowser_LoadCompleted"
                                                                ScriptNotify="WebBrowser_ScriptNotify">
                          
</phone:WebBrowser>
 
private void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
                if (WebBrowser.IsScriptEnabled)
                {
                                var JavaScriptText =
                                                @"function ReplaceBadXmlChars(str) {
                                                var stri = str.split('&').join('&amp;');
                                                stri = stri.split('<').join('&lt;');
                                                stri = stri.split('>').join('&gt;');
                                                stri = stri.split("'").join('&apos;');
                                                stri = stri.split('"').join('&quot;');
                                                return stri;
                                }
 
                                function FindParentByTag(item, tag) {
                                                if (!item.parentNode) return null;
                                                if (item.tagName.toLowerCase() == tag.toLowerCase()) {
                                                                return item;
                                                } else {
                                                                return FindParentByTag(item.parentNode, tag);
                                                }
                                }
 
                                function OnClick() {
                                                var linkItem = FindParentByTag(event.srcElement, 'a');
                                                var imageItem = FindParentByTag(event.srcElement, 'img');
 
                                                var zoom = screen.deviceXDPI / screen.logicalXDPI;
                                                var valid = false;
                                                var notifyMsg = '<Click ';
                                                {
                                                                notifyMsg += 'pos="' + parseInt(event.clientX * zoom) + ',' + parseInt(event.clientY * zoom) + '" ';
                                                                if (linkItem != null && linkItem.href != null && !linkItem.href.startsWith("javascript")) {
                                                                                notifyMsg += 'url="' + ReplaceBadXmlChars(linkItem.href) + '" ';
                                                                                valid = true;
                                                                }
 
                                                                if (imageItem != null && imageItem.href != null && !linkItem.href.startsWith("javascript")) {
                                                                                notifyMsg += 'img="' + ReplaceBadXmlChars(imageItem.src) + '" ';
                                                                                valid = true;
                                                                }
                                                }
                                                notifyMsg += '/>';
                                                if (valid) {
                                                                window.external.notify(notifyMsg);
                                                                return false;
                                                }
                                }
 
 
                                function RegisterClickNotification() {
                                                window.document.onclick = OnClick;
 
                                                window.document.body.addEventListener('MSPointerDown', function(evt) {
                                                                evt = evt || window.event;
 
                                                                var linkItem = FindParentByTag(evt.srcElement, 'a');
                                                                var imageItem = FindParentByTag(evt.srcElement, 'img');
 
                                                                window.devicePixelRatio = window.screen.deviceXDPI / window.screen.logicalXDPI;
                                                                var doc = window.document.documentElement;
                                                                var left = (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0);
                                                                var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
 
                                                                var timerId = window.setTimeout(function() {
                                                                                var notifyMsg = 'pos="' + parseInt((evt.clientX - left)) + ',' + parseInt((evt.clientY - top)) + '" ';
 
                                                                                if (linkItem != null && linkItem.href != null) {
                                                                                                notifyMsg += 'url="' + ReplaceBadXmlChars(linkItem.href) + '" ';
                                                                                }
 
                                                                                if (imageItem != null && imageItem.href != null) {
                                                                                                notifyMsg += 'img="' + ReplaceBadXmlChars(imageItem.src) + '" ';
                                                                                }
 
                                                                                window.external.notify('<Hold ' + notifyMsg + '/>');
                                                                }, 500);
                                                                StopLoading();
                                                                evt.target.data = timerId;
                                                });
                                }
 
                                window.document.body.addEventListener('MSPointerUp', function (evt) {
                                                window.clearTimeout(evt.target.data);
                                });
 
                                window.document.body.addEventListener('MSPointerMove', function (evt) {   
                                                window.clearTimeout(evt.target.data);   
                                });
 
                                window.document.body.addEventListener('MSPointerOut', function (evt) {
                                                window.clearTimeout(evt.target.data);
                                });
 
                                window.document.body.addEventListener('pointerup', function (evt) {
                                 
                                });"
 
                                WebBrowser.InvokeScript("eval", new string[] { JavaScriptText });
                                WebBrowser.InvokeScript("RegisterClickNotification");
                                WebBrowser.InvokeScript("execScript", new string[] {
                                "function eventListener(evt) {if (evt.type == 'MSPointerDown'){ gestureHandler.addPointer(evt.pointerId); return; } if (evt.detail & evt.MSGESTURE_FLAG_END) {  window.external.notify(evt.srcElement.tagName);}}" });
                                WebBrowser.InvokeScript("execScript", new string[] { "document.addEventListener('MSGestureHold', eventListener, false); document.addEventListener('MSPointerDown', eventListener, false);  gestureHandler = new MSGesture(); gestureHandler.target = document.body;" });
                }
}

答案 3 :(得分:0)

以下代码有效,但即使您点按任何图片或链接也会触发。我们可能不得不添加某种计时器,因为我们无法附加。

编辑:

我能够弄明白!以下代码适用于WP8并且仅检测保留,而不是检测。

public void AttachScripts()
    {
        try
        {
            if (GINternet.IsScriptEnabled)
            {
                var scriptsText = @"function ReplaceBadXmlChars(str) {
                                            var stri = str.split('&').join('&amp;');
                                            stri = stri.split('<').join('&lt;');
                                            stri = stri.split('>').join('&gt;');
                                            stri = stri.split(""'"").join('&apos;');
                                            stri = stri.split('""""').join('&quot;');
                                            return stri;
                            }

                            function FindParentLink(item) {
                            if (!item.parentNode)
                                return null;
                            if (item.tagName.toLowerCase() == 'a') {
                                return item;
                            } else {
                                return FindParentLink(item.parentNode);
                            }}

                            function FindParentImage(item) {
                            if (!item.parentNode)
                                return null;
                            if (item.tagName.toLowerCase() == 'img') { 
                                return item;
                            } else {
                                return FindParentImage(item.parentNode);
                            }}

                            var currGNetMouseTimer;
                            window.document.body.addEventListener('pointerdown', function (evt) {
                                                            evt = evt || window.event;

                                                            var linkItem = FindParentLink(evt.srcElement);
                                                            var imageItem = FindParentImage(evt.srcElement);

                                                            currGNetMouseTimer = window.setTimeout(function() {
                                                                            var notifyMsg = '';

                                                                            if (linkItem != null && linkItem.href != null) {
                                                                                notifyMsg += ReplaceBadXmlChars(linkItem.href);
                                                                            }

                                                                            if (imageItem != null && imageItem.src != null) {
                                                                                notifyMsg += ReplaceBadXmlChars(imageItem.src);
                                                                            }

                                                                            if (notifyMsg != '') {
                                                                                window.external.notify(notifyMsg);
                                                                            } else {
                                                                                window.external.notify('NOTLINKIMG');
                                                                            }
                                                            }, 750);
                            },false);

                            window.document.body.addEventListener('pointermove', function (evt) {
                                            window.clearTimeout(currGNetMouseTimer);
                            },false); 

                            window.document.body.addEventListener('pointerup', function (evt) {
                                            window.clearTimeout(currGNetMouseTimer);
                            },false); 

                            window.document.body.addEventListener('pointerout', function (evt) {
                                            window.clearTimeout(currGNetMouseTimer);
                            },false);";



                GINternet.InvokeScript("execScript", new string[] { scriptsText });
            }
        }
        catch
        {
        }
    }

    private void GINternet_LoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
    {
        //REQUIRED FOR WINDOWS PHONE 8
        AttachScripts();
}