[英]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 我想从Windows Phone 8中的html页面调用ac#方法。目前我正在尝试使用以下代码,但它无法正常工作
JavaScript: JavaScript的:
<input id="Button1" type="button" value="button" onclick="window.external.something();" />
and my c# method in mainpage.xaml 和mainpage.xaml中的c#方法
void something()
{
MessageBox.Show("called from javascript");
}
can any one tell me the way to call ac# code behind method from java-script. 任何人都可以告诉我从java脚本调用ac#code behind方法的方法。 ie how to pass variables from javascript to c# method and vice-versa.
即如何将变量从javascript传递到c#方法,反之亦然。
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. 您必须使用
window.external.notify("string")
并且必须在后面的代码中注册浏览器的ScriptNotify
事件才能接收呼叫。
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: 要执行反向(从后面的代码调用JavaScript中的函数),您可以:
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;
通过设置
MyWebBrowser.IsScriptEnabled = true;
确保您的Web浏览器可以使用JavaScript MyWebBrowser.IsScriptEnabled = true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.