如何在通用 Windows 应用程序的 WebView 中调用 javascript 函数 [英] How to invoke javascript functions in a WebView in Universal Windows App
问题描述
我是通用应用开发的新手.任何人都可以帮助我进入以下代码,
I am new in Universal app development. Can any one help me into following code,
我在通用应用程序中使用 Web 视图控件加载了一个网站.我想从同一个网站读取一些控制值.
I have load one website using web view control in universal app. I want to read some control value from same website.
我的标签控件 ID 在网站上是lblDestination".
My label control id is 'lblDestination' on website.
我正在通用应用程序中访问它,例如MAinPage.xaml
I am accessing this in universal app like
MAinPage.xaml
<WebView x:Name="Browser" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Loaded="Browser_Loaded"
NavigationFailed="Browser_NavigationFailed">
</WebView>
MAinPage.xaml.cs
Browser.InvokeScript("eval", new string[] { "document.getElementById('lblDestination')" }).ToString()
这是读取浏览器控件值的正确方法吗?我正在使用模拟器来测试这个应用程序,那么是模拟器造成了问题吗?
Is this right way to read the browser control value? I am using the simulator for testing this app, So is simulator creating the problem?
推荐答案
我不确定您何时何地使用 InvokeScript
与 JavaScript eval
函数注入内容进入网页.但通常我们可以使用 WebView.DOMContentLoaded
事件.当 WebView 完成对当前 HTML 内容的解析时会发生此事件,因此我们可以通过此事件确保 HTML 内容已准备就绪.
I'm not sure where and when you use InvokeScript
with the JavaScript eval
function to inject content into the web page. But usually we can use WebView.DOMContentLoaded
event. This event occurs when the WebView has finished parsing the current HTML content so with this event we can make sure the HTML content is prepared.
如果我们想在 Windows 10 通用应用程序的 WebView 内容中调用 JavaScript,我们最好使用 WebView.InvokeScriptAsync
方法 as
And If we want to invoke JavaScript inside the WebView content in Windows 10 Universal app, we'd better use WebView.InvokeScriptAsync
method as
[InvokeScript 可能会在 Windows 8.1 之后的版本中更改或不可用.而是使用 InvokeScriptAsync.]
[InvokeScript may be altered or unavailable for releases after Windows 8.1. Instead, use InvokeScriptAsync.]
最后但并非最不重要的一点,请注意 InvokeScriptAsync
方法只能返回脚本调用的字符串结果.
Last but not least, please do note that the InvokeScriptAsync
method can only return the string result of the script invocation.
被调用的脚本只能返回字符串值.
The invoked script can return only string values.
所以如果你的 JavaScript 的返回值不是字符串,WebView.InvokeScriptAsync
方法的返回值将是一个空字符串.
So if the return value of your JavaScript is not a string, the return value of WebView.InvokeScriptAsync
method will be a empty string.
如果你使用
var value = await Browser.InvokeScriptAsync("eval", new string[] { "document.getElementById('lblDestination')" });
该值将是一个空字符串,因为 document.getElementById('lblDestination')
返回一个 Element
.
the value will be a empty string as document.getElementById('lblDestination')
returns an Element
.
所以要读取一些控制值,您可以尝试使用如下代码:
So to read some control value, you can try using code like following:
var innerText = await Browser.InvokeScriptAsync("eval", new string[] { "document.getElementById('lblDestination').innerText" });
如果你想要获取的值不是字符串,你可能需要先在 JavaScript 中将其转换为字符串.例如:
And if the value you want to get is not a string, you may need to convert it to string in JavaScript first. For example:
var childElementCount = await Browser.InvokeScriptAsync("eval", new string[] { "document.getElementById('lblDestination').childElementCount.toString()" });
这篇关于如何在通用 Windows 应用程序的 WebView 中调用 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!