C# 如何在WebView中使用InvokeScript处理JS事件
为了让C#从WebView中提取信息或对事件做出反应,我们使用InvokeScript。但是,InvokeScript似乎没有将属性附加到事件。怎么了C# 如何在WebView中使用InvokeScript处理JS事件,c#,javascript,windows-8,webview,winrt-xaml,C#,Javascript,Windows 8,Webview,Winrt Xaml,为了让C#从WebView中提取信息或对事件做出反应,我们使用InvokeScript。但是,InvokeScript似乎没有将属性附加到事件。怎么了 private void WebView_LoadCompleted(object sender, NavigationEventArgs e) { var _WebView = sender as WebView; _WebView.AllowedScriptNotifyUris = WebView.AnyScriptNotif
private void WebView_LoadCompleted(object sender, NavigationEventArgs e)
{
var _WebView = sender as WebView;
_WebView.AllowedScriptNotifyUris = WebView.AnyScriptNotifyUri;
_WebView.AllowedScriptNotifyUris.Add(e.Uri);
// test1 fires immediately
_WebView.InvokeScript("execScript", new[] { "window.external.notify('test1'); " });
// test2 never fires
_WebView.InvokeScript("execScript", new[] { "document.addEventListener = function (e) { window.external.notify('test2'); }" });
// test3 never fires
_WebView.InvokeScript("execScript", new[] { "function x(e) { window.external.notify('test3'); } document.addEventListener('onmousedown', x);" });
}
private void WebView_ScriptNotify(object sender, NotifyEventArgs e)
{
// listen for external.notify event
System.Diagnostics.Debugger.Break();
}
我觉得这不像javascript。更多的c#?@IanOverton你需要阅读实际问题。
\u WebView.InvokeScript(
是javascript。document.addEventListener=function(e){window.external.notify('test2');}
您是否有意重写addEventListener
?我认为应该是document.onmousedown=function…
或window.onmousedown=function…