/// <summary> /// Gets the JSValue or creates it if not found /// </summary> /// <returns>The or create.</returns> /// <param name="ctx">Context.</param> /// <param name="Name">Name.</param> public static JSValue GetOrCreate(this JSContext ctx, string Name) { var aClass = ctx [new NSString(Name)]; if (aClass == null || aClass.IsUndefined) { ctx.SetObject(new NSObject(), Name); aClass = ctx [new NSString(Name)]; } return(aClass); }
public static JSValue BuildNamespace(this JSContext ctx, String Namespace) { //split the namespace into sections var names = Namespace.Split('.'); JSValue jsObject = null; foreach (var aName in names) { if (jsObject == null) { var anObject = ctx [new NSString(aName)]; if (anObject != null) { jsObject = anObject; } else { //creat an object and load it back ctx.SetObject(new NSObject(), aName); jsObject = ctx [new NSString(aName)]; } } else { var anObject = jsObject [new NSString(aName)]; if (anObject != null) { jsObject = anObject; } else { //creat an object and load it back jsObject.SetObject(new NSObject(), aName); jsObject = jsObject [new NSString(aName)]; } } } return(jsObject); }
public override void DidCreateJavaScriptContext(UIWebView webView, JSContext ctx) { if (ctx != null) { //set a value this.ANumber = 20; //set an NSObject value in to the context ctx.SetObject(webView, @"webView"); ctx.SetObject(this, @"webDelegate"); ///Attach a hybrid api handler DSHybridHandler.Attach(ctx); //load a property ctx.SetObject(ANumber, @"aNumber"); //attach object to namespace ctx.SetObject("webView", "aNumber", ANumber); //create an object that uses DSJavascriptObject as a base classes, which in turn exposes class members using JSExport //Note this can onl be done with the protocol in Obj-c not monotouch ctx.SetObject(new aClass(), @"aClass"); //attach extra property to the aclass object ctx.SetObject("aClass", "subNumber", ANumber); //set a execution block that can accept a number and return a number ctx.SetNumberBlock((NSObject num) => { var toInt = num as NSNumber; var aInt = toInt.IntValue; return(new NSNumber(aInt * 2)); }, @"getInt"); //Set an execution block in the context, 1 parameter no return type ctx.SetBlock((obj) => { BeginInvokeOnMainThread(() => { //convert to string var name = obj.ToString(); //create message var message = String.Format("Hello, {0}!", name); var aNewAler = new UIAlertView("Welome", message, null, "OK", null); aNewAler.Show(); }); }, @"sayHello"); ///Multi-parameter void block ctx.SetBlock((obj, obj2) => { BeginInvokeOnMainThread(() => { var firstName = obj.ToString(); var lastName = obj2.ToString(); var message = String.Format("Hello, {0}{1}!", firstName, lastName); var aNewAler = new UIAlertView("Welome", message, null, "OK", null); aNewAler.Show(); }); }, @"sayFirstAndLastName"); } }
public override void DidCreateJavaScriptContext (UIWebView webView, JSContext ctx) { if (ctx != null) { //set a value this.ANumber = 20; //set an NSObject value in to the context ctx.SetObject (webView, @"webView"); ctx.SetObject (this, @"webDelegate"); ///Attach a hybrid api handler DSHybridHandler.Attach (ctx); //load a property ctx.SetObject (ANumber, @"aNumber"); //attach object to namespace ctx.SetObject ("webView", "aNumber", ANumber); //create an object that uses DSJavascriptObject as a base classes, which in turn exposes class members using JSExport //Note this can onl be done with the protocol in Obj-c not monotouch ctx.SetObject (new aClass (), @"aClass"); //attach extra property to the aclass object ctx.SetObject ("aClass", "subNumber", ANumber); //set a execution block that can accept a number and return a number ctx.SetNumberBlock ((NSObject num) => { var toInt = num as NSNumber; var aInt = toInt.IntValue; return new NSNumber (aInt * 2); }, @"getInt"); //Set an execution block in the context, 1 parameter no return type ctx.SetBlock ((obj) => { BeginInvokeOnMainThread (() => { //convert to string var name = obj.ToString (); //create message var message = String.Format ("Hello, {0}!", name); var aNewAler = new UIAlertView ("Welome", message, null, "OK", null); aNewAler.Show (); }); }, @"sayHello"); ///Multi-parameter void block ctx.SetBlock ((obj, obj2) => { BeginInvokeOnMainThread (() => { var firstName = obj.ToString (); var lastName = obj2.ToString (); var message = String.Format ("Hello, {0}{1}!", firstName, lastName); var aNewAler = new UIAlertView ("Welome", message, null, "OK", null); aNewAler.Show (); }); }, @"sayFirstAndLastName"); } }