Sunday, April 13, 2014

Jint tutorial 007 - Using Jint and Jint.Ex in an iOS Application

Reminder : Jint is a Javascript v 5 interpreter for the .NET Framework/Mono/Xamarin allowing to execute scripts in C# applications on Windows, MacOS, iOS, Android and Linux (Anywhere there is a C# 4.x compiler).

Jint.Ex is event-driven interaction run time for Jint to build non blocking UI and creating asynchronous API.

In this example, I ported the WinForm app described in Jint tutorial 006 - Using Jint and Jint.Ex in a Win Form Application to an iOS application using the Xamarin stack.

For this sample, I used the MonoTouch Dialog library to build the UI.
The overall C# code is the same which is the point of Jint.Ex all the JavaScript execution including callback event are executed in a background thread by Jint.Ex AsyncronousEngine and interact with the UI.




C# 

   

namespace Jint.Ex.iOS {
    
    [Register("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate {
        
        UINavigationController navigation;
        UIWindow window;
        private AsyncronousEngine _asyncronousEngine;
        private Section _outputSection;

        private void InitAsyncronousEngine() {

            _asyncronousEngine = new AsyncronousEngine();
            _asyncronousEngine.EmbedScriptAssemblies.Add(Assembly.GetExecutingAssembly());
            // Expose to the JavaScript the function setUserMessage which add a string in the Listbox 
            _asyncronousEngine.Engine.SetValue("setUserMessage", new Action<string>(__setUserMessage__));
        }

        private void __setUserMessage__(string s) {
       
                InvokeOnMainThread (delegate {                    
                        _outputSection.Insert (0, UITableViewRowAnimation.None, new StringElement (s));
                });
        }

        public void SynchronousExecution() {
            _asyncronousEngine.RequestFileExecution("SynchronousExecution.js");
        }

        public void AsynchronousExecution() {
            _asyncronousEngine.RequestFileExecution("AsynchronousExecution.js");
        }

        public void Timer() {
            _asyncronousEngine.RequestFileExecution("Timer.js");
        }

        public void MultipleTimers() {
            _asyncronousEngine.RequestFileExecution("MultipleTimers.js");
        }

        public void ClearOuput() {
            _outputSection.RemoveRange(0, _outputSection.Elements.Count);
        }

        public void ClearEventQueue() {
            ClearOuput();
            _asyncronousEngine.RequestClearQueue();
        }

        public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
            InitAsyncronousEngine();

            _outputSection = new Section("Output") {

            };

            var menu = new RootElement ("Jint and Jint.Ex iOS Demo") {
                new Section ("Menu"){
                    new StringElement ("Synchronous Execution", SynchronousExecution),
                    new StringElement ("Asynchronous Execution", AsynchronousExecution),
                    new StringElement ("Timer", Timer),
                    new StringElement ("Multiple Timer", MultipleTimers),
                    new StringElement ("Clear Output", ClearOuput),
                    new StringElement ("Clear Event Queue", ClearEventQueue),
                },
                _outputSection
            };

            var dv = new DialogViewController (menu) {
                Autorotate = true
            };
            navigation = new UINavigationController ();
            navigation.PushViewController (dv, true);               

            // On iOS5 we use the new window.RootViewController, on older versions, we add the subview
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            window.MakeKeyAndVisible ();
            if (UIDevice.CurrentDevice.CheckSystemVersion (5, 0))
                window.RootViewController = navigation; 
            else
                window.AddSubview (navigation.View);

            return true;
        }
    }

}

No comments:

Post a Comment