Mah*_*esh 4 c# win-universal-app uwp uwp-xaml
我是Universal app开发的新手.任何人都可以帮助我编写代码,
我在通用应用程序中使用Web视图控件加载了一个网站.我想从同一个网站上读取一些控制值.
我的标签控件ID是网站上的'lblDestination'.
我在通用应用程序中访问此类似的 
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()
这是读取浏览器控件值的正确方法吗?我正在使用模拟器来测试这个应用程序,那么模拟器是否会产生问题?
我不确定在何时何地使用InvokeScriptJavaScript eval函数将内容注入网页.但通常我们可以使用WebView.DOMContentLoaded事件.当WebView完成解析当前HTML内容时会发生此事件,因此通过此事件,我们可以确保准备好HTML内容.
如果我们想调用Javascript中的WebView在Windows 10的通用应用程序的内容,我们最好使用WebView.InvokeScriptAsync方法为
[ 在Windows 8.1之后,InvokeScript可能会被更改或不可用于发行版.相反,使用InvokeScriptAsync.]
最后但并非最不重要的是,请注意该InvokeScriptAsync方法只能返回脚本调用的字符串结果.
调用的脚本只能返回字符串值.
因此,如果JavaScript的返回值不是字符串,则WebView.InvokeScriptAsyncmethod 的返回值将为空字符串.
如果你使用
var value = await Browser.InvokeScriptAsync("eval", new string[] { "document.getElementById('lblDestination')" });
document.getElementById('lblDestination')返回一个值为空字符串Element.
因此,要读取一些控制值,您可以尝试使用以下代码:
var innerText = await Browser.InvokeScriptAsync("eval", new string[] { "document.getElementById('lblDestination').innerText" });
如果您想要获取的值不是字符串,则可能需要先将其转换为JavaScript中的字符串.例如:
var childElementCount = await Browser.InvokeScriptAsync("eval", new string[] { "document.getElementById('lblDestination').childElementCount.toString()" });