简体   繁体   中英

how to call a windows phone 8 code-behind method from html input button click

i want to call ac# method from html page in windows phone 8. currently i am trying with the following code but it is not working

JavaScript:

 <input id="Button1" type="button" value="button" onclick="window.external.something();" />

and my c# method in mainpage.xaml

void something()
{
MessageBox.Show("called from javascript");
}

can any one tell me the way to call ac# code behind method from java-script. ie how to pass variables from javascript to c# method and vice-versa.

You have to use window.external.notify("string") and have to register to the ScriptNotify event of the browser in the code behind in order to receive the call.

You register this way:

MyWebBrowser.ScriptNotify+=OnScriptNotify;

You consume this way:

OnScriptNotify(object sender, NotifyEventArgs e){
    //do something e.Value contains the string passed as a parameter
}

To do the inverse (calling a function in JavaScript from code behind) you do:

MyWebBrowser.InvokeScript("functionName");
MyWebBrowser.InvokeScript("functionWithParam", "param1", "param2");

Those function return an object.

Be sure to make your web browser work with JavaScript by setting MyWebBrowser.IsScriptEnabled = true;

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