简体   繁体   中英

How to pass parameter from javascript to C# method in windows phone hybrid application

I am creating a hybrid windows phone application in which I need to pass parameter from javascript function in html to c# method through script.notify

HTML

<html>
<body onload="junk('hello')">
    <script>
        function junk(name) {
            window.external.notify("Hello " + name);
        }
    </script>
</body>
</html>

My question is how to get this particular method's parameter junk() in C#

If I use below code , I am receiving all the parameters from all the methods but I need parameters from junk() method alone

void webBrowser_ScriptNotify(object sender, NotifyEventArgs e)
{
    MessageBox.Show(e.Value);
}

You will receive all script notifications (from all methods) from within the WebBrowser control, because your event handler webBrowser_ScriptNotify is linked to that WebBrowser:

A code example taken from Matthias Shapiro's blog (though you're already doing this, but it's missing from your information above)


We can tell our app to handle that script launch either in the XAML of our WebBrowser:

<phone:WebBrowser x:Name="Browser" ScriptNotify="HTML_Script_Launched" />

Or by assigning it in the xaml.cs file

Browser.ScriptNotify += HTML_Script_Launched;

Ofcourse your handler is called 'webBrowser_ScriptNotify' instread of 'HTML_Script_Launched'.

So in webBrowser_ScriptNotify you will have to determine from which method/source the window.external.notify was done.

Maybe the NotifyEventArgs e object contains additional information? (I haven't looked into this myself)

Or you can define your own format to send both the message (from the junk() method in this case) and details about where it came from; a sort of "meta data" if you will.

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