public void RegisterFunctions(JSObject window) { var type = typeof(string); var jsInterop = window.AddObject("jsInterop", JSInvokeMode.Invoke); var methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static).Where(m => !m.IsSpecialName && m.IsDefined(typeof(JSInteropAttribute), false)); foreach (var method in methods) { HandlerMap.Add(method.Name, method); jsInterop.AddFunction(method.Name, JSInvokeMode.Invoke).Execute += InvokeHandler; } var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static).Where(m => m.IsDefined(typeof(JSInteropAttribute), false)); foreach (var field in fields) { var dp = jsInterop.AddDynamicProperty(field.Name, JSInvokeMode.Invoke); FieldMap.Add(field.Name, field); dp.PropertyGet += PropertyGet; dp.PropertySet += PropertySet; } }
public void Register(JSObject @object) { //注册模块 var funcObj = @object.AddObject("V8Demo"); TestLogin(funcObj); }
public static void UseSetProjectPathPage(this JSObject chromfx, Package <Store> store) { var uploadPageActions = chromfx.AddObject("setProjectPathActions"); uploadPageActions.AddFunction("setProjectPath").Execute += (func, args) => { store.Dispatch(new SetProjectPath()); }; }
protected override void OnRegisterGlobalObject(JSObject global) { var myObject = global.AddObject("my"); var htmlCloseWindow = myObject.AddFunction("htmlCloseWindow"); htmlCloseWindow.Execute += HtmlCloseWindow_Execute; var htmlTitleMouseDown = myObject.AddFunction("htmlTitleMouseDown"); htmlTitleMouseDown.Execute += HtmlTitleMouseDown_Execute; var htmlTitleMouseUp = myObject.AddFunction("htmlTitleMouseUp"); htmlTitleMouseUp.Execute += HtmlTitleMouseUp_Execute; }
public static void RegisterJSObject(this JSObject jsObject, object clrObject) { var objectType = clrObject.GetType(); JSObjectAttribute objectDescriber = (JSObjectAttribute)objectType.GetCustomAttributes(typeof(JSObjectAttribute), true).FirstOrDefault(); var jsObjectName = objectType.Name; if (objectDescriber != null && !string.IsNullOrEmpty(objectDescriber.JSName)) { jsObjectName = objectDescriber.JSName; } var thisObject = jsObject.AddObject(jsObjectName); RegisterJSObjectProperties(thisObject, clrObject); }
public static void UseCreateProjectPage(this JSObject chromfx, Package <Store> store, ChromFXBaseForm chromFXBaseForm) { var pluginPageActions = chromfx.AddObject("createProjectActions"); pluginPageActions.AddFunction("startProcess").Execute += (func, args) => { var str = args.Arguments[0].StringValue; var modelProcess = JsonConvert.DeserializeObject <List <ModelProcess> >(str); var state = store.GetState(); Parallel.ForEach(modelProcess, ele => { store.Dispatch(new StartProcess() { ModelProcess = ele, SetProjectPathPageState = state.SetProjectPathPageState, RefreshState = (model) => { var curValue = JsonConvert.SerializeObject(model); string cmd = string.Format("createProjectActions.refreshUpload(\"{0}\",{1})", model.Id, model.Percent); chromFXBaseForm.ExecuteJavascript(cmd); }, ShowMessage = (m) => { store.Dispatch(new ShowMessage() { Message = m }); } }); }); }; pluginPageActions.AddFunction("loadMoudels").Execute += (func, args) => { store.Dispatch(new LoadMoudels()); }; }
public bool runTools(string name) { if (dllList.ContainsKey(name)) { return(false); } //挂载dll Assembly asm = Assembly.LoadFrom("tools\\" + name + "\\" + "ui.dll"); //读取类型 Type t = asm.GetType(name + ".Main"); //创建对象信息 object o = Activator.CreateInstance(t); Bootstrap.RegisterAssemblyResources(asm, null, name + ".greycloud.com"); ITools it = new ITools(name, asm, t, ToolsManager.AddObject(name), o); dllList.Add(name, it); //配置 Type del1 = t.GetNestedType("d_registerFunc"); if (del1 != null) { try { Delegate d = Delegate.CreateDelegate(del1, this.GetType().GetMethod("registerFunc")); t.GetField("registerFunc").SetValue(o, d); } catch (ArgumentException e) { Debug.WriteLine(e.ParamName); } } //执行Load MethodInfo m_Load = t.GetMethod("Load", new Type[] { typeof(int) }); bool result = (bool)m_Load.Invoke(o, new object[] { 0 }); return(result); }
public static void UsePluginPage(this JSObject chromfx, Package <Store> store) { var pluginPageActions = chromfx.AddObject("pluginPageActions"); pluginPageActions.AddFunction("createProjectJson").Execute += (func, args) => { var str = args.Arguments[0].StringValue; var projectCreateMoudle = JsonConvert.DeserializeObject <ProjectCreateMoudle>(str); var state = store.GetState(); state.CreateProjectPageState.Models = new List <ModelProcess>(); state.CreateProjectPageState.Models.Add(new ModelProcess() { Id = projectCreateMoudle.Project.Id, Name = projectCreateMoudle.Project.Name, DownloadPath = "", Engine = projectCreateMoudle.Project.Engine, OutputPath = "", IsTemplateProject = true, Version = projectCreateMoudle.Project.Version, Des = projectCreateMoudle.Project.Des }); foreach (var item in projectCreateMoudle.FuncModels) { state.CreateProjectPageState.Models.Add(new ModelProcess() { Id = item.Id, Name = item.Name, DownloadPath = "", Engine = item.Engine, OutputPath = "", IsTemplateProject = false, Version = item.Version, Des = item.Des }); } store.Dispatch(new CreateProjectJson() { ProjectCreateMoudle = projectCreateMoudle }); }; pluginPageActions.AddFunction("loadEngins").Execute += (func, args) => { store.Dispatch(new LoadEngins()); }; pluginPageActions.AddFunction("loadEnginVersions").Execute += (func, args) => { store.Dispatch(new LoadEnginVersions()); }; pluginPageActions.AddFunction("loadFuncs").Execute += (func, args) => { var engin = args.Arguments[0].StringValue; var version = args.Arguments[1].StringValue; var queryStr = args.Arguments[2].StringValue; store.Dispatch(new LoadFuncs() { Engine = engin, Version = version, FuncName = queryStr }); }; pluginPageActions.AddFunction("loadProjects").Execute += (func, args) => { store.Dispatch(new LoadProjects()); }; }
public virtual void Register(JSObject obj) { Self = obj.AddObject(ObjectName); }
public static void UseUploadPage(this JSObject chromfx, Package <Store> store) { var uploadPageActions = chromfx.AddObject("uploadPageActions"); uploadPageActions.AddFunction("uploadFolder").Execute += (func, args) => { var fileStr = args.Arguments[0].StringValue; var file = JsonConvert.DeserializeObject <FileState>(fileStr); var doZipAction = store.asyncAction <FileState, bool>(async(dispatcher, store2, fileState) => { try { dispatcher(new DoZip() { File = fileState }); var curFile = store.GetState().UploadPageState.Files.FirstOrDefault(p => p.OriginalFileFullPath == fileState.OriginalFileFullPath && p.IsZiped); return(curFile != null); } catch (Exception ex) { return(false); } }); var doZipRes = store.Dispatch(doZipAction(file)).Result; if (doZipRes) { store.Dispatch(new StartUpload() { File = file }); store.Dispatch(new DoUpload() { File = file }); } }; uploadPageActions.AddFunction("uploadProjectFolder").Execute += (func, args) => { var fileStr = args.Arguments[0].StringValue; var file = JsonConvert.DeserializeObject <FileState>(fileStr); var doZipAction = store.asyncAction <FileState, bool>(async(dispatcher, store2, fileState) => { try { dispatcher(new DoProjectZip() { File = fileState }); var curFile = store.GetState().UploadPageState.ProjectFiles.FirstOrDefault(p => p.OriginalFileFullPath == fileState.OriginalFileFullPath && p.IsZiped); return(curFile != null); } catch (Exception ex) { return(false); } }); var doZipRes = store.Dispatch(doZipAction(file)).Result; if (doZipRes) { store.Dispatch(new StartProjectUpload() { File = file }); store.Dispatch(new DoProjectUpload() { File = file }); } }; uploadPageActions.AddFunction("selectFolder").Execute += (func, args) => { store.Dispatch(new SelectFolder()); }; uploadPageActions.AddFunction("selectProjectFolder").Execute += (func, args) => { store.Dispatch(new SelectProjectFolder()); }; }
// 浏览器Javascript环境初始化完成 protected override void OnRegisterGlobalObject(JSObject global) { // 可以在此处将C#对象注入到当前窗口的JS上下文中 //add a function with callback function Console.Write(global); var callbackTestFunc = global.AddFunction("callbackTest"); callbackTestFunc.Execute += (func, args) => { var callback = args.Arguments.FirstOrDefault(p => p.IsFunction); if (callback != null) { WebBrowser.ExecuteJavascript("ChangeTitle()"); var callbackArgs = CfrV8Value.CreateObject(new CfrV8Accessor()); callbackArgs.SetValue("success", CfrV8Value.CreateBool(true), CfxV8PropertyAttribute.ReadOnly); callbackArgs.SetValue("text", CfrV8Value.CreateString("Message from Windows Client"), CfxV8PropertyAttribute.ReadOnly); callback.ExecuteFunction(null, new CfrV8Value[] { callbackArgs }); } }; //register the "my" object var myObject = global.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) => { string computerName = Environment.MachineName; string value = $"My Computer Name is JoyNop :)\n{computerName}"; // getter - if js code "my.name" executes, it'll get the string "NanUI". args.Retval = CfrV8Value.CreateString(value); 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) { string osVersionName = Environment.OSVersion.ToString(); MessageBox.Show(osVersionName, "Windows 内核版本", MessageBoxButtons.OK, MessageBoxIcon.Information); } }; var friends = new string[] { "Mr.JSON", "Mr.Lee", "Mr.BONG" }; 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" } }; }