2013-05-10 111 views
0

我試圖在用戶在網頁上的鏈接上執行掛起手勢時附加上下文菜單。向WP8瀏覽器控件添加上下文菜單

我在網上搜索,發現了一些建議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瀏覽器控件?

編輯

我發現的信息是window.navigator.msPointerEnabled是WebBrowser控件和True上的Internet Explorer應用程序錯誤。那麼這意味着我們無法在控件中正確實現觸摸事件檢測。我們可以將其設置爲啓用嗎?

回答

0

如果window.navigator.msPointerEnabled是假的,你可以使用onmousedown事件和onmouseup監測

0

試圖獲得WP8的Web瀏覽器的控制做一些技巧,這裏有一對夫婦的意見後。

的...

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

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

這並沒有真正回答這個問題。如果您有不同的問題,可以通過單擊[提問](http://stackoverflow.com/questions/ask)來提問。您還可以[添加賞金](http://stackoverflow.com/help/privileges/set-bounties)在您擁有足夠的[聲譽](http://stackoverflow.com/help/)時吸引更多人關注此問題什麼聲譽)。 – 2015-03-11 13:07:09

+0

我已編輯並試圖提供更好的答案。 – 2015-03-11 16:44:41

相關問題