如何在Universal Windows App中的WebView中调用javascript函数 [英] How to invoke javascript functions in a WebView in Universal Windows App

查看:58
本文介绍了如何在Universal Windows App中的WebView中调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是通用应用开发的新手.谁能帮助我编写以下代码,

I am new in Universal app development. Can any one help me into following code,

我已经在通用应用中使用网络视图控件加载了一个网站.我想从同一网站上读取一些控制值.

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?

推荐答案

我不确定您何时何地使用

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 Universal应用程序的 WebView 内容内调用JavaScript,则最好使用

And If we want to invoke JavaScript inside the WebView content in Windows 10 Universal app, we'd better use WebView.InvokeScriptAsync method as

在Windows 8.1之后的版本中,

[ InvokeScript "可能已更改或不可用.而是使用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()" });

这篇关于如何在Universal Windows App中的WebView中调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆