【问题标题】:Cannot run simple script with WebView Universal Windows App无法使用 WebView 通用 Windows 应用程序运行简单脚本
【发布时间】:2016-07-04 15:31:01
【问题描述】:

我将简单的 html 文件(保存在项目中)加载到 WebView。它加载成功并显示“Hello World”标题。

文件 index.html

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>ABC</title>
<script>
    function SayHello() {
        return "hello";
    };
</script>
</head>
<body>
    <h1>Hello World</h1>
</body>
</html>

加载到 WebView

 protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            webView.Navigate(new Uri("ms-appx-web:///index.html", UriKind.RelativeOrAbsolute));

// Exception below line
            var data = await webView.InvokeScriptAsync("eval", new List<string> { "SayHello();" });
            Debug.WriteLine(data);
        }

我不明白为什么会这样。它在谷歌浏览器上成功运行。我错过了什么吗?

【问题讨论】:

  • 异常详情是什么?
  • 只是 System.Exception

标签: windows-phone win-universal-app


【解决方案1】:

MSDN关于Navigate方法:

在指定的统一资源标识符处加载 HTML 内容 (URI)。

当您调用该方法时,它不会立即加载内容,可能需要一些时间。您需要监听DOMContentLoaded 事件,然后在页面实际加载到 WebView 时运行脚本。

webView1.DOMContentLoaded += webView1_DOMContentLoaded;
...
...
private async void webView1_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
   var data = await webView.InvokeScriptAsync("eval", new List<string> { "SayHello();" });
   Debug.WriteLine(data);
}

【讨论】:

    【解决方案2】:

    我认为您应该转到 URI 中的清单配置文件, Windows 运行时访问 = 全部 类型 = 包括

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多