我試圖在用戶在網頁上的鏈接上執行掛起手勢時附加上下文菜單。向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應用程序錯誤。那麼這意味着我們無法在控件中正確實現觸摸事件檢測。我們可以將其設置爲啓用嗎?
這並沒有真正回答這個問題。如果您有不同的問題,可以通過單擊[提問](http://stackoverflow.com/questions/ask)來提問。您還可以[添加賞金](http://stackoverflow.com/help/privileges/set-bounties)在您擁有足夠的[聲譽](http://stackoverflow.com/help/)時吸引更多人關注此問題什麼聲譽)。 – 2015-03-11 13:07:09
我已編輯並試圖提供更好的答案。 – 2015-03-11 16:44:41