简体   繁体   中英

Communication between HTML in WebBrowser and windows phone app

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.

So my question is,

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?

For receiving data from HTML to phone:

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.

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

in CS:

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

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

For passing data from phone to HTML:

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

in javascript: function doSomething(args) { }

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM