WebBrowser 中的 HTML 与 Windows Phone 应用程序之间的通信 [英] Communication between HTML in WebBrowser and windows phone app

查看:21
本文介绍了WebBrowser 中的 HTML 与 Windows Phone 应用程序之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些静态 html 文件下载到独立商店.我正在尝试使用 Windows 手机应用程序中的 Web 浏览器控件打开这些文件.现在每个 HTML 文件都会通过 javascript 与后端代码通信,并且 window.external.notify() 并不是所有的 html 页面都可以应用.

I have some static html files downloaded into Isolated store. I am trying to open those files using Web Browser control inside windows phone app. Now every HTML file will communicate with back-end code through javascript, and window.external.notify() is not able to apply for all html pages.

所以我的问题是,

是否有任何 API 像(在 iOS 中)WebViewJavascriptBridgeGAJavaScript 或类似 (android) Javascript Interface 可用于 windows phone 8,它将与 WebBrowser 和 windows phone 中的 HTML 通信应用程序.WinJS 是否也支持 Windows Phone 8?

Is there any API like (in iOS) WebViewJavascriptBridge and GAJavaScript or like (android) Javascript Interface available for windows phone 8 which will communicate with HTML in WebBrowser and windows phone app. Will WinJS supports the same for Windows Phone 8?

推荐答案

用于从 HTML 接收数据到手机:

For receiving data from HTML to phone:

您需要为 WebBrowser 的 ScriptNotify 事件添加事件处理程序.之后,每当您在 html 页面内调用 window.external.Notify(args) 时,ScriptNotify 都会触发,您将在 NotifyEventArgs 参数的 Value 属性中获取 args.

You need to add event handler for ScriptNotify event of WebBrowser. After that, whenever you call window.external.Notify(args) inside html page, ScriptNotify will fire and you'll get the args in Value property of NotifyEventArgs parameter.

在javascript中:window.external.Notify(args);

in javascript: window.external.Notify(args);

在 CS:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    browser.ScriptNotify += browser_ScriptNotify;
}

private void browser_ScriptNotify(object sender, NotifyEventArgs e)
{
    var valueFromBrowser = e.Value;
}

用于将数据从手机传递到 HTML:

For passing data from phone to HTML:

您需要使用 WebBrowser 的 InvokeScript() 方法.在那里你需要在 javascript 中传递函数的名称和输入参数(如果有).

You need to use InvokeScript() method of WebBrowser. There you need to pass name of the function in javascript and input parameters if any.

在javascript中:function doSomething(args) { }

in javascript: function doSomething(args) { }

在 CS 中:browser.InvokeScript("doSomething", args);

希望有帮助.

这篇关于WebBrowser 中的 HTML 与 Windows Phone 应用程序之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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