如何调用Windows phone 8代码隐藏方法从HTML输入按钮点击

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

本文关键字:方法 隐藏 HTML 输入 按钮 代码 何调用 调用 phone Windows      更新时间:2023-09-26

我想从Windows phone 8的HTML页面调用一个c#方法。目前,我正在尝试使用以下代码,但它不工作

JavaScript:

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

和我的c#方法在mainpage.xaml

void something()
{
MessageBox.Show("called from javascript");
}
有谁能告诉我如何从java-script中调用c#代码隐藏方法吗?例如,如何将变量从javascript传递到c#方法,反之亦然。

您必须使用window.external.notify("string"),并且必须在后面的代码中注册到浏览器的ScriptNotify事件,以便接收调用。

您可以这样注册:

MyWebBrowser.ScriptNotify+=OnScriptNotify;

你这样消费:

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

要做相反的事情(从后面的代码调用JavaScript中的函数),您可以这样做:

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

这些函数返回一个对象。

通过设置MyWebBrowser.IsScriptEnabled = true;

,确保您的web浏览器与JavaScript一起工作