添加的ContextMenu到WP8浏览器控件 [英] Adding contextmenu to WP8 browser control

查看:238
本文介绍了添加的ContextMenu到WP8浏览器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图附加上下文菜单时用户在网页上的链接上执行的手势搁置

I am trying to attach a context menu when user perform a hold gesture on a link on a webpage.

我在网上搜索,发现了一些建议<一href=\"http://social.msdn.microsoft.com/Forums/en-US/wpdevelop/thread/56f5ef15-f2b5-439f-bbf2-babfe591fef3/\"相对=nofollow>这里

I've searched the web and found some recommendations 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提出这个误差

But the second execScript raised this error

 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: 

我也试过以下,在此基础上<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/wpdevelop/thread/73dad68e-9cf3-4413-8d51-f8294ad18f36/\"相对=nofollow>发布。但很显然它只能在WP7手机的工作不是在WP8或模拟器。

I've also tried the following, based on this posting. But apparently it only work in WP7 phone not in WP8 or the emulator.

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浏览器控件?

Anyone know how to attach context menu in WP8 browser control?

修改

我发现一个信息的 window.navigator.msPointerEnabled 是WebBrowser控件和True上的Internet Explorer应用程序错误的。这是否意味着我们不能正确实现在控制触摸事件检测。我们可以将其设置为启用?

I've found an info that window.navigator.msPointerEnabled is false on the WebBrowser control and True on the Internet Explorer application. So does that mean we can't implement touch event detection properly in the control. Can we set it to enabled?

推荐答案

以下code ++工程,但即使你的图像或链接,点击它触发。我们可能需要添加一些,因为我们不能附加的计时器。

The following code works, but it fires even when you tap on any image or link. We may have to add a timer of some sort since we can't attach.

编辑:

我能看着办吧!下面code在WP8工程和只检测一抱,没有自来水。

I was able to figure it out! The following code works in WP8 AND only detects a hold, not a tap.

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();
}

这篇关于添加的ContextMenu到WP8浏览器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆