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?
here's my class so far.
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
}
You cannot. Codebehind is running on the server while JavaScript is running on the client.
The first method is to call the JavaScript function in HTML. For this, you have to create a function then define this function either in the head section or body section of the HTML document. You can either create a link or a button and then an onclick() event is associated with them in order to call this function.
No, you can't trigger JavaScript from CSS directly. What you can do is use CSS selectors to find the elements you want to watch in this way, and then watch for mouse events.
The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.
Try:
webBrowser.InvokeScript("myFunction", "one", "two", "three");
InvokeScript
executes a scripting function defined in the currently loaded document, and passes the function an array of string parameters.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402838%28v=vs.105%29.aspx
Obviously you have to have the JavaScript
function defined in the loaded document.
Depending on your view it may work like this:
this.CordovaView.Browser.InvokeScript("eval", new string[] { "yourJavascriptFunction(); " });
I found a solution, admittedly not the best one but works for me.
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;
}
}
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);
}
Once the WebView is set I can then call InvokeScript from any other class:
WebViewHandler.getInstance().webView.CordovaBrowser.InvokeScript("MyJavaScriptFunctionThatIWishToCall");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With