How to call using JavaScript with C # - Cordova / PhoneGap

I use cordova / phonegap to create an application for a Windows phone, I try to call a script from C # when an event occurs.

Is there any way to do this?

here is my class.

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:\n" + httpChannel.ChannelUri.ToString()); SaveChannelInfo(); Trace("Subscribing to channel events"); SubscribeToService(); SubscribeToNotifications(); // SEND CHANNEL URI TO JAVASCRIPT } 
+7
javascript c # cordova windows-phone-8
source share
4 answers

I found the solution, admittedly, not the best, but works for me.

I created a singleton WebViewHandler class that looks like

 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; } } 

Then I install webview in the constructor on HomePage like this:

  public HomePage() { InitializeComponent(); CordovaView.Loaded += CordovaView_Loaded; WebViewHandler.getInstance().setWebView(ref CordovaView); } 

After installing WebView, I can call InvokeScript from any other class:

 WebViewHandler.getInstance().webView.CordovaBrowser.InvokeScript("MyJavaScriptFunctionThatIWishToCall"); 
+4
source share

Try:

 webBrowser.InvokeScript("myFunction", "one", "two", "three"); 

InvokeScript executes the script function defined in the currently loaded document and passes an array of string parameters to the functions.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402838%28v=vs.105%29.aspx

Obviously, you must have the JavaScript function defined in the loaded document.

Depending on your type, this may work as follows:

 this.CordovaView.Browser.InvokeScript("eval", new string[] { "yourJavascriptFunction(); " }); 
+9
source share

You can use DispatchCommandResult (); as indicated in the cord documentation. This way you can call the C # method, send everything you need in the callback, and then just execute javascript from javascript.

+1
source share

Try this example:

 string str="<script>alert(\"ok\");</script>"; Page.ClientScript.RegisterStartupScript(this.GetType(), "Script", str, false); 
0
source share

All Articles