Windows 10 WebView 调用脚本不工作
Windows 10 WebView InvokeScript Not Working
我的 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 及更早版本上完美运行。但是现在用Windows10的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 更新的东西?
Windows10 中的 InvokeScript 仅适用于可信来源。我试过了,得到了和你一样的结果。之前在 Win 8 上工作的确切脚本不再在 UWP 上工作。
事实证明,解决方案非常简单。显然 InvokeScript 已在 Windows 10 UWP 中弃用,现在只有 InvokeScriptAsync 可用。所以我所要做的就是使我的方法成为异步方法,然后等待 InvokeScriptAsync 调用。
我的 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 及更早版本上完美运行。但是现在用Windows10的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 更新的东西?
Windows10 中的 InvokeScript 仅适用于可信来源。我试过了,得到了和你一样的结果。之前在 Win 8 上工作的确切脚本不再在 UWP 上工作。
事实证明,解决方案非常简单。显然 InvokeScript 已在 Windows 10 UWP 中弃用,现在只有 InvokeScriptAsync 可用。所以我所要做的就是使我的方法成为异步方法,然后等待 InvokeScriptAsync 调用。