从使用window.external.notify调用的C#函数返回一个值
本文关键字:返回 一个 函数 window external notify 调用 | 更新日期: 2023-09-27 18:19:38
我正在使用phonegap为windows phone 8制作一个应用程序。我使用window.external.Notify([commandname])
从javascript中调用了C#函数。它正确地调用了C#代码。但我想将C#代码中的字符串值返回给那个javascript函数。用于解决方案-我试着用InvokeScript()
方法调用一个javascript函数,并将结果作为参数传递给它。但它给了我OutOfMemoryException
。
我的C#代码是-
public MainPage()
{
InitializeComponent();
this.CordovaView.Loaded += CordovaView_Loaded;
CordovaView.Browser.ScriptNotify+=Browser_ScriptNotify;
}
private void Browser_ScriptNotify(object sender, NotifyEventArgs e)
{
string commandStr = e.Value.ToString();
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
//get the key data
if (commandStr == kToken + "-get" || commandStr == kMSISDN + "-get" || commandStr == kTermsAccepted + "-get" || commandStr == kUserDetails + "-get")
{
string[] keys = commandStr.Split('-');
key = keys[0];
if (!settings.Contains(key))
{
sampledata = null;
}
else
{
sampledata = (string)settings[key];
}
this.CordovaView.Browser.IsScriptEnabled = true;
this.CordovaView.Browser.InvokeScript("mydata", data);
}
else(commandStr == "removedata")
{
settings.Clear();
}
}
private void CordovaView_Loaded(object sender, RoutedEventArgs e)
{
this.CordovaView.Loaded -= CordovaView_Loaded;
}
}
}
下面是我的javascript代码-
function getDataForKey(key) {
window.external.Notify(key + '-get');
senddata: return data;
}
function mydata(inputdata)
{
alert(inputdata);
data = inputdata;
alert('Invoked script is running');
goto: senddata;
}
请帮我解决这个问题。我是phonegap的新手。所以我不知道如何从javascript调用C#,反之亦然。我也在phonegap文档中寻找解决方案,但没有得到任何缓解。提前谢谢。
如果你想使用PhoneGap/Cordova并以独立于平台的方式完成你想要做的事情,Cordova插件机制会有所帮助。您应该实现自己的cordova插件:
请参阅插件开发指南
这里提供了windows本机端实现的详细信息。