【问题标题】:Windows 10 WebView InvokeScript Not WorkingWindows 10 WebView InvokeScript 不工作
【发布时间】:2016-03-18 04:56:08
【问题描述】:

我的 Windows 10 通用应用程序正在使用一个 webview,它使用 InvokeScript 调用 JS 函数“eval”,同时将一些其他 javascript 注入该函数。但是,当我测试它时,什么也没有发生。调试显示脚本被正确存储并且它正在命中函数,但 webview 似乎根本没有更新。

private void browser1_LoadCompleted_1(object sender, NavigationEventArgs e)
    {
        try
        {
            count++;
            browser1.InvokeScript("eval", test);
        }
        catch (Exception E)
        {

        }

        address.Text = browser1.Source.ToString();
    }
}

此代码在 Windows Phone 8 及更早版本上运行良好。但是现在用Windows 10 webview,好像没什么效果。这是将我的脚本存储在测试变量中的代码:

private async void ReadFile()
    {
        string fileContent;
        StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///Script.js"));
        using (StreamReader sRead = new StreamReader(await file.OpenStreamForReadAsync()))
            fileContent = await sRead.ReadToEndAsync();
        test = new string[] { fileContent };
    }

我不确定为什么什么也没发生。我已经确认测试变量在调用 InvokeScript 时确实包含脚本。我什至使用不同且更简单的 JS 脚本测试了 InvokeScript,这些脚本在该应用程序的 Windows Phone 8 版本中肯定有效。当调用 JS 函数时,我是否缺少让 webview 更新的东西?

【问题讨论】:

    标签: c# windows windows-phone windows-10 windows-10-universal


    【解决方案1】:

    原来解决方案非常简单。显然 InvokeScript 在 Windows 10 UWP 中已被弃用,现在只有 InvokeScriptAsync 有效。所以我要做的就是让我的方法成为异步方法,然后等待 InvokeScriptAsync 调用。

    【讨论】:

      【解决方案2】:

      Windows 10 中的 InvokeScript 仅适用于受信任的来源。我试过了,结果和你一样。之前在 Win 8 上运行的确切脚本不再适用于 UWP。

      【讨论】:

      • 那么这是否意味着 InvokeScript 在 UWP 上几乎什么都不做?或者有没有办法让它成为一个值得信赖的来源?
      猜你喜欢
      • 2012-05-05
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2019-02-09
      • 2018-06-07
      • 2014-01-09
      • 2018-07-22
      • 1970-01-01
      相关资源
      最近更新 更多