protected override void Dispose(bool disposing) { if (!string.IsNullOrEmpty(DisposeFun)) { var es = MBApi.wkeGlobalExec(WebView); var fun = MBApi.jsEvalW(es, string.Format(RETURN_FUN, DisposeFun)); MBApi.jsCallGlobal(es, fun, null, 0); } base.Dispose(disposing); }
private static T Call <T>( long i_webView, long i_control, long cancelPtr, string jsFunction, FunctionParamterCollection paramterCollection) { IntPtr controlPtr = new IntPtr(i_control); Control control = Control.FromHandle(controlPtr); return((T)control.Invoke(new Func <long, T>(w => { try { IntPtr wV = new IntPtr(w); var es = MBApi.wkeGlobalExec(wV); var args = paramterCollection.ToJsValue(es); var script = string.Format(FUNCTION_CALL_FORMAT, jsFunction); var funV = MBApi.jsEvalW(es, script); if (MBApi.jsIsUndefined(funV)) { return default(T); } long resV = MBApi.jsCallGlobal(es, funV, args, args.Length); var obj = JsConvert.ConvertJSToObject(es, resV, typeof(T)); if (obj == null) { return default(T); } return (T)obj; } catch (Exception ex) { return default(T); } }), i_webView)); }
/// <summary> /// 计算表达式 /// </summary> /// <param name="jsExecState"></param> /// <param name="script"></param> /// <returns></returns> public static JsValue Eval(IntPtr jsExecState, string script) { return(MBApi.jsEvalW(jsExecState, script)); }
public static object ConvertJSToObject(IntPtr es, long v, Type objType) { var pt = MBApi.jsTypeOf(v); if (pt == jsType.UNDEFINED) { return(null); } else if (pt == jsType.FUNCTION) { string funData = ExtApi.GetJsString(es, v); //TODO:创建动态委托 return(FunctionCreater.CreateJsFunctionCallBack(MBApi.jsGetWebView(es), funData, objType)); } else if (pt == jsType.BOOLEAN) { return(MBApi.jsToBoolean(es, v)); } else if (pt == jsType.NUMBER) { return(Convert.ChangeType(MBApi.jsToDouble(es, v), objType)); } else if (pt == jsType.STRING) { return(ExtApi.GetJsString(es, v)); } else if (MBApi.jsIsArray(v)) { var eType = objType.GetElementType(); var len = MBApi.jsGetLength(es, v); var args = Array.CreateInstance(eType, len); for (int i = 0; i < len; i++) { var argV = ConvertJSToObject(es, MBApi.jsGetAt(es, v, i), eType); args.SetValue(argV, i); } return(args); } else if (pt == jsType.OBJECT) { var base_obj = Activator.CreateInstance(objType); var pInfos = objType.GetProperties(); var dicFun = MBApi.jsEvalW(es, SCRIPT_FUN); //将object转化成{name,value}的数组 var dv = MBApi.jsCallGlobal(es, (long)dicFun, new long[] { v }, 1); var dLen = MBApi.jsGetLength(es, dv); for (int i = 0; i < dLen; i++) { var item = MBApi.jsGetAt(es, dv, i); var name = ExtApi.GetJsString(es, MBApi.jsGet(es, item, NAME_ATTR)); PropertyInfo pInfo = null; if (pInfos.Length > 0) { foreach (var x in pInfos) { if (x.Name.ToLower() == name.ToLower()) { pInfo = x; break; } } } if (pInfo != null) { var js_obj = MBApi.jsGet(es, item, VALUE_ATTR); var obj = ConvertJSToObject(es, js_obj, pInfo.PropertyType); pInfo.SetValue(base_obj, obj, null); } } return(base_obj); } else { return(null); } }