Exemplo n.º 1
0
        private void InitJsObject()
        {
            var noteUtilsJsObj = GlobalObject.AddObject("NoteUtils");

            var hideWinFormCsFunc = noteUtilsJsObj.AddFunction("HideWinForm");

            hideWinFormCsFunc.Execute += HideWinFormCsFunc_Execute;

            var getTitleFormCSFunc = noteUtilsJsObj.AddFunction("GetTitle");

            getTitleFormCSFunc.Execute += GetTitleFormCSFunc_Execute;

            var setNoteToFormCSFunc = noteUtilsJsObj.AddFunction("SaveNewNote");

            setNoteToFormCSFunc.Execute += SetNoteToFormCSFunc_Execute;

            var getNoteContentFormCSFunc = noteUtilsJsObj.AddFunction("GetNoteContent");

            getNoteContentFormCSFunc.Execute += GetNoteContentFormCSFunc_Execute;

            var setNoteContentFormCSFunc = noteUtilsJsObj.AddFunction("SetNoteContent");

            setNoteContentFormCSFunc.Execute += SetNoteContentFormCSFunc_Execute;

            var SetHistoryNoteContentFormCSFunc = noteUtilsJsObj.AddFunction("SetHistoryNoteContent");

            SetHistoryNoteContentFormCSFunc.Execute += SetHistoryNoteContentFormCSFunc_Execute;

            var UpdateNoteTitleFormCSFunc = noteUtilsJsObj.AddFunction("UpdateNoteTitle");

            UpdateNoteTitleFormCSFunc.Execute += UpdateNoteTitleFormCSFunc_Execute;

            var DeleteNoteTitleFormCSFunc = noteUtilsJsObj.AddFunction("DeleteNoteTitle");

            DeleteNoteTitleFormCSFunc.Execute += DeleteNoteTitleFormCSFunc_Execute;

            var GetNoteContentHistoryFormCSFunc = noteUtilsJsObj.AddFunction("GetNoteContentHistory");

            GetNoteContentHistoryFormCSFunc.Execute += GetNoteContentHistoryFormCSFunc_Execute;

            var GetHistoryContentFormCSFunc = noteUtilsJsObj.AddFunction("GetHistoryContent");

            GetHistoryContentFormCSFunc.Execute += GetHistoryContentFormCSFunc_Execute;

            var CloseFormCSFunc = noteUtilsJsObj.AddFunction("Close");

            CloseFormCSFunc.Execute += CloseFormCSFunc_Execute;

            var SelectDataPathFormCSFunc = noteUtilsJsObj.AddFunction("SelectDataPath");

            SelectDataPathFormCSFunc.Execute += SelectDataPathFormCSFunc_Execute;

            var GetConfigFormCSFunc = noteUtilsJsObj.AddFunction("GetConfig");

            GetConfigFormCSFunc.Execute += GetConfigFormCSFunc_Execute;

            var SetConfigFormCSFunc = noteUtilsJsObj.AddFunction("SetConfig");

            SetConfigFormCSFunc.Execute += SetConfigFormCSFunc_Execute;
        }
Exemplo n.º 2
0
        public IDisk() : base("http://res.app.local/asserts/index.html")
        {
            InitializeComponent();

            Constant.Win = this;

            LoadHandler.OnLoadStart += LoadHandler_OnLoadStart;
            LoadHandler.OnLoadEnd   += LoadHandler_OnLoadEnd;

            //register the "my" object
            var myObject = GlobalObject.AddObject("my");

            BosConfigControl bosConfigControl = new BosConfigControl();

            bosConfigControl.addFunction(myObject);

            CloudFileControl cloudFileControl = new CloudFileControl();

            cloudFileControl.addFunction(myObject);

            DownloadRecordControl downloadRecordControl = new DownloadRecordControl();

            downloadRecordControl.addFunction(myObject);

            ConstantControl.addFunction(myObject);
        }
Exemplo n.º 3
0
        public Form1() : base("http://tools.greycloud.com/www/new.html")
        {
            //加载配置信息
            ci = JsonConvert.DeserializeObject <c_info>(info);
            ct = JsonConvert.DeserializeObject <C_Tools>(tools);

            InitializeComponent();

            //绑定事件
            LoadHandler.OnLoadStart += LoadHandler_OnLoadStart;
            DragHandler.OnDragEnter += DragHandler_OnDragEnter;
            LoadHandler.OnLoadEnd   += LoadHandler_OnLoadEnd;

            //greyTools_api
            var greyTools_api = GlobalObject.AddObject("greyTools_api");
            var setColor      = greyTools_api.AddFunction("setColor");

            setColor.Execute += (func, args) =>
            {
                var stringArgument = args.Arguments.FirstOrDefault(p => p.IsString);
                if (stringArgument != null)
                {
                    this.BorderColor = System.Drawing.ColorTranslator.FromHtml(stringArgument.StringValue);
                }
            };

            //windowsApi
            var windows_api = GlobalObject.AddObject("windows_api");
            var openWeb     = windows_api.AddFunction("openWeb");

            openWeb.Execute += (func, args) =>
            {
                var stringArgument = args.Arguments.FirstOrDefault(p => p.IsString);
                if (stringArgument != null)
                {
                    System.Diagnostics.Process.Start(stringArgument.StringValue);
                }
            };

            //Tools管理器
            ToolsManager = GlobalObject.AddObject("ToolsManager");
            var runTools = ToolsManager.AddFunction("runTools");

            runTools.Execute += (func, args) =>
            {
                var stringArgument = args.Arguments.FirstOrDefault(p => p.IsString);
                if (stringArgument != null)
                {
                    bool result = this.runTools(stringArgument.StringValue);
                    args.SetReturnValue(CfrV8Value.CreateBool(result));
                }
            };
        }
Exemplo n.º 4
0
        private void RegisterObject()
        {
            List <IBaseObject> listObject = new List <IBaseObject>();
            var repository = GlobalObject.AddObject("CS");

            listObject.Add(new AuthObject());
            listObject.Add(new AppObject.AppObject());
            listObject.Add(new DBObject());
            listObject.Add(new HotelObject(this));
            listObject.Add(new SettingObject());

            repository.AddDynamicProperty("IsDebug").PropertyGet += IsDebug_PropertyGet;

            foreach (var obj in listObject)
            {
                obj.Register(repository);
            }
        }
Exemplo n.º 5
0
        public Login()
            : base(AnimationType.Center, "wwwroot/Pages/Login.html", false)
        {
            InitializeComponent();
            base.ContextMenuHandler.OnBeforeContextMenu += (sender, e_) => e_.Model.Clear();  //禁用右键
            this.MinimumSize = new Size(475, 335);
            this.MaximumSize = new Size(475, 335);
            //this.FormBorderStyle = FormBorderStyle.FixedToolWindow;

            //注册显示注册界面事件到JS
            base.GlobalObject.AddFunction("showRegistered").Execute += (_, args) =>
            {
                this.RequireUIThread(() =>
                {
                    if (registered == null || registered.IsDisposed)
                    {
                        registered = new Registered();
                        registered.Show();
                    }
                    else
                    {
                        //if (registered.WindowState == FormWindowState.Minimized)
                        //    registered.WindowState = FormWindowState.Normal;
                        registered.Activate();
                    }
                    //Mian main = new Mian();
                    //main.Show();
                });
            };

            //注册关闭事件到JS
            base.GlobalObject.AddFunction("closeForm").Execute += (_, args) =>
            {
                this.RequireUIThread(() =>
                {
                    this.Close();
                    this.Dispose();
                    Application.Exit();
                });
            };

            //注册最小化事件到JS
            base.GlobalObject.AddFunction("minForm").Execute += (_, args) =>
                                                                this.RequireUIThread(() =>
                                                                                     this.WindowState = FormWindowState.Minimized);

            //注册显示主界面窗体到JS
            base.GlobalObject.AddFunction("viewMain").Execute += (_, args) =>
            {
                this.RequireUIThread(() =>
                {
                    //Application.Run(new Main());
                    this.Close();
                    this.Dispose();
                    //this.DialogResult = DialogResult.OK;
                    Mian main = new Mian();
                    main.Show();

                    //this.Close();
                });
            };
            var myObject = GlobalObject.AddObject("my");
            var nameProp = myObject.AddDynamicProperty("name");

            nameProp.PropertyGet += (prop, args) =>
            {
                args.Retval = CfrV8Value.CreateString("MyLogin");
                args.SetReturnValue(true);
            };
            nameProp.PropertySet += (prop, args) =>
            {
                var value = args.Value;
                args.SetReturnValue(true);
            };


            /*登录*/
            var FuncLogin = myObject.AddFunction("Func_login");

            FuncLogin.Execute += (func, args) =>
            {
                var stringArgument = args.Arguments.FirstOrDefault(p => p.IsString);
                if (stringArgument != null)
                {
                    var     str   = stringArgument.StringValue;
                    JObject model = JObject.Parse(str);

                    ReturnMessageModel returnMessage = LoginFun(model);


                    var resultStr = CfrV8Value.CreateString(Newtonsoft.Json.JsonConvert.SerializeObject(returnMessage));
                    args.SetReturnValue(resultStr);
                }
            };



            //注册验证码到JS
            base.GlobalObject.AddFunction("ClickValidateCode").Execute += (_, args) =>
            {
                string base64 = BaseImgCode();
                ExecuteJavascript("ImgCode('data:image/jpg;base64," + base64 + "')");
            };

            //加载获取验证码
            LoadHandler.OnLoadEnd += LoadValidaeCode;


            //google调试器
            base.LoadHandler.OnLoadStart += (sender, e) =>
            {
                base.Chromium.ShowDevTools();
            };
        }
Exemplo n.º 6
0
        public Form1()
            : base("http://res.app.local/www/index.html")
        {
            InitializeComponent();

            LoadHandler.OnLoadEnd += LoadHandler_OnLoadEnd;

            //register the "my" object
            var myObject = GlobalObject.AddObject("my");

            //add property "name" to my, you should implemnt the getter/setter of name property by using PropertyGet/PropertySet events.
            var nameProp = myObject.AddDynamicProperty("name");

            nameProp.PropertyGet += (prop, args) =>
            {
                // getter - if js code "my.name" executes, it'll get the string "NanUI".
                args.Retval = CfrV8Value.CreateString("NanUI");
                args.SetReturnValue(true);
            };
            nameProp.PropertySet += (prop, args) =>
            {
                // setter's value from js context, here we do nothing, so it will store or igrone by your mind.
                var value = args.Value;
                args.SetReturnValue(true);
            };


            //add a function showCSharpMessageBox
            var showMessageBoxFunc = myObject.AddFunction("showCSharpMessageBox");

            showMessageBoxFunc.Execute += (func, args) =>
            {
                //it will be raised by js code "my.showCSharpMessageBox(`some text`)" executed.
                //get the first string argument in Arguments, it pass by js function.
                var stringArgument = args.Arguments.FirstOrDefault(p => p.IsString);

                if (stringArgument != null)
                {
                    MessageBox.Show(this, stringArgument.StringValue, "C# Messagebox", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            };

            //add a function getArrayFromCSharp, this function has an argument, it will combind C# string array with js array and return to js context.
            var friends = new string[] { "Mr.JSON", "Mr.Lee", "Mr.BONG" };

            var getArrayFromCSFunc = myObject.AddFunction("getArrayFromCSharp");

            getArrayFromCSFunc.Execute += (func, args) =>
            {
                var jsArray = args.Arguments.FirstOrDefault(p => p.IsArray);



                if (jsArray == null)
                {
                    jsArray = CfrV8Value.CreateArray(friends.Length);
                    for (int i = 0; i < friends.Length; i++)
                    {
                        jsArray.SetValue(i, CfrV8Value.CreateString(friends[i]));
                    }
                }
                else
                {
                    var newArray = CfrV8Value.CreateArray(jsArray.ArrayLength + friends.Length);

                    for (int i = 0; i < jsArray.ArrayLength; i++)
                    {
                        newArray.SetValue(i, jsArray.GetValue(i));
                    }

                    var jsArrayLength = jsArray.ArrayLength;

                    for (int i = 0; i < friends.Length; i++)
                    {
                        newArray.SetValue(i + jsArrayLength, CfrV8Value.CreateString(friends[i]));
                    }


                    jsArray = newArray;
                }


                //return the array to js context

                args.SetReturnValue(jsArray);

                //in js context, use code "my.getArrayFromCSharp()" will get an array like ["Mr.JSON", "Mr.Lee", "Mr.BONG"]
            };

            //add a function getObjectFromCSharp, this function has no arguments, but it will return a Object to js context.
            var getObjectFormCSFunc = myObject.AddFunction("getObjectFromCSharp");

            getObjectFormCSFunc.Execute += (func, args) =>
            {
                //create the CfrV8Value object and the accssor of this Object.
                var jsObjectAccessor = new CfrV8Accessor();
                var jsObject         = CfrV8Value.CreateObject(jsObjectAccessor);

                //create a CfrV8Value array
                var jsArray = CfrV8Value.CreateArray(friends.Length);

                for (int i = 0; i < friends.Length; i++)
                {
                    jsArray.SetValue(i, CfrV8Value.CreateString(friends[i]));
                }

                jsObject.SetValue("libName", CfrV8Value.CreateString("NanUI"), CfxV8PropertyAttribute.ReadOnly);
                jsObject.SetValue("friends", jsArray, CfxV8PropertyAttribute.DontDelete);


                args.SetReturnValue(jsObject);

                //in js context, use code "my.getObjectFromCSharp()" will get an object like { friends:["Mr.JSON", "Mr.Lee", "Mr.BONG"], libName:"NanUI" }
            };


            //add a function with callback

            var callbackTestFunc = GlobalObject.AddFunction("callbackTest");

            callbackTestFunc.Execute += (func, args) => {
                var callback = args.Arguments.FirstOrDefault(p => p.IsFunction);
                if (callback != null)
                {
                    var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor());
                    callbackArgs.SetValue("success", CfrV8Value.CreateBool(true), CfxV8PropertyAttribute.ReadOnly);
                    callbackArgs.SetValue("text", CfrV8Value.CreateString("Message from C#"), CfxV8PropertyAttribute.ReadOnly);

                    callback.ExecuteFunction(null, new CfrV8Value[] { callbackArgs });
                }
            };


            //add a function with async callback
            var asyncCallbackTestFunc = GlobalObject.AddFunction("asyncCallbackTest");

            asyncCallbackTestFunc.Execute += async(func, args) => {
                //save current context
                var v8Context = CfrV8Context.GetCurrentContext();
                var callback  = args.Arguments.FirstOrDefault(p => p.IsFunction);

                //simulate async methods.
                await Task.Delay(5000);

                if (callback != null)
                {
                    //get render process context
                    var rc = callback.CreateRemoteCallContext();

                    //enter render process
                    rc.Enter();

                    //create render task
                    var task = new CfrTask();
                    task.Execute += (_, taskArgs) =>
                    {
                        //enter saved context
                        v8Context.Enter();

                        //create callback argument
                        var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor());
                        callbackArgs.SetValue("success", CfrV8Value.CreateBool(true), CfxV8PropertyAttribute.ReadOnly);
                        callbackArgs.SetValue("text", CfrV8Value.CreateString("Message from C#"), CfxV8PropertyAttribute.ReadOnly);

                        //execute callback
                        callback.ExecuteFunction(null, new CfrV8Value[] { callbackArgs });


                        v8Context.Exit();

                        //lock task from gc
                        lock (task)
                        {
                            Monitor.PulseAll(task);
                        }
                    };

                    lock (task)
                    {
                        //post task to render process
                        v8Context.TaskRunner.PostTask(task);
                    }

                    rc.Exit();

                    GC.KeepAlive(task);
                }
            };
        }