0

为了让 C# 从 WebView 中提取信息或对事件做出反应,我们使用 InvokeScript。但是,InvokeScript 似乎没有属性附加到事件。怎么了?

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

0 回答 0