Ajout du menu contextuel au contrôle du navigateur WP8

J'essaie d'attacher un menu contextuel lorsque l'utilisateur effectue un geste de retenue sur un lien sur une page Web.

J'ai cherché sur le Web et trouvé quelques recommandations ICI

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

Mais le second execScript a soulevé cette erreur

  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: 

J'ai également essayé ce qui suit, en fonction de cette publication . Mais apparemment, cela ne fonctionne que dans le téléphone WP7, pas dans WP8 ou l'émulateur.

 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 { } } 

Je surveille le résultat via ScriptNotify mais il n'a jamais déclenché

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

Quelqu'un sait comment attacher un menu contextuel au contrôle du navigateur WP8?

MODIFIER

J'ai trouvé une information que window.navigator.msPointerEnabled est false sur le contrôle WebBrowser et True sur l'application Internet Explorer. Cela signifie donc que nous ne pouvons pas implémenter correctement la détection d'événement tactile dans le contrôle. Pouvons-nous le configurer?

Si window.navigator.msPointerEnabled est faux, vous pouvez utiliser onmousedown et onmouseup pour surveiller

Après avoir essayé d'obtenir le contrôle du navigateur WP8, faites quelques astuces, voici quelques observations.

Le …

System.SystemException
Message = Une erreur inconnue s'est produite. Erreur: 80020101.

… signifie généralement que votre javascript n'a pas été correctement analysé. Souvent, une erreur de syntaxe.

J'ai découvert que les caractères CR, LF et TAB réduisent le bruit et rendent les erreurs de syntaxe disponibles dans une fenêtre d'éditeur JavaScript VisualStudio un peu plus facile. String.Replace () est votre ami.

Je n'ai réussi à utiliser InvokeScript () pour exécuter des fonctions anonymes. Ce modèle a réussi: (attention à la ponctuation)

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

Dans votre cas particulier, je vois que trois fonctions sont définies, mais que les causes sont-elles exécutées? Peut-être que cela explique l'exception.

Gyle

C'est ma solution. Il fonctionne sur 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;" });               } } 

Le code suivant fonctionne, mais il se déclenche même lorsque vous appuyez sur une image ou un lien. Nous devrons peut-être ajouter une minuterie, car nous ne pouvons pas nous joindre.

MODIFIER:

J'ai pu le comprendre! Le code suivant fonctionne dans WP8 ET ne détecte qu'une prise, pas un robinet.

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