如何从 C# 调用 JavaScript - Cordova/PhoneGap [英] How to call JavaScript from C# - Cordova/PhoneGap
本文介绍了如何从 C# 调用 JavaScript - Cordova/PhoneGap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用cordova/phonegap 来制作一个Windows 手机应用程序,当一个事件触发时,我试图从C# 调用一个脚本.
I'm using cordova/phonegap to make a windows phone app, i'm trying to call a script from C# when an event fires.
有没有办法做到这一点?
Is there anyway to do this?
这是我目前的课程.
public void register(string options)
{
// This is executed asynchronously
if (!TryFindChannel())
DoConnect();
}
void httpChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
// Finished asynchronous task in "register" method
Trace("Channel opened. Got Uri:
" + httpChannel.ChannelUri.ToString());
SaveChannelInfo();
Trace("Subscribing to channel events");
SubscribeToService();
SubscribeToNotifications();
// SEND CHANNEL URI TO JAVASCRIPT
}
推荐答案
我找到了一个解决方案,虽然不是最好的,但对我有用.
I found a solution, admittedly not the best one but works for me.
我创建了一个名为 WebViewHandler 的单例类,它看起来像这样
I created a singleton class called WebViewHandler which looks like this
class WebViewHandler
{
private static WebViewHandler instance;
public bool isWebViewReady { get { return webView != null; } }
public WPCordovaClassLib.CordovaView webView;
private WebViewHandler()
{
}
public void setWebView(ref WPCordovaClassLib.CordovaView webView)
{
this.webView = webView;
}
public static WebViewHandler getInstance()
{
if(instance == null){
instance = new WebViewHandler();
}
return instance;
}
}
然后我在 HomePage 的构造函数中设置 webview,如下所示:
Then I set the webview in the constructor on the HomePage like so:
public HomePage()
{
InitializeComponent();
CordovaView.Loaded += CordovaView_Loaded;
WebViewHandler.getInstance().setWebView(ref CordovaView);
}
一旦设置了 WebView,我就可以从任何其他类调用 InvokeScript:
Once the WebView is set I can then call InvokeScript from any other class:
WebViewHandler.getInstance().webView.CordovaBrowser.InvokeScript("MyJavaScriptFunctionThatIWishToCall");
这篇关于如何从 C# 调用 JavaScript - Cordova/PhoneGap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文