Beispiel #1
0
        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");
			}
		}