protected void DataBoundControl_DataBinding(object sender, EventArgs e) { var parameterModel = new DataBindParameterModel(Offset); foreach (var parameters in Parameters) { var values = parameters.Value.GetValues(HttpContext.Current, DataBoundControl); foreach (Parameter parameter in parameters.Value) { var field = GetField(parameter); var defaultValue = GetDefaultValue(parameter); parameterModel.DataBindParameters.Add(new DataBindParameter(field, parameter.GetType().Name.Replace("Parameter", null), values[parameter.Name], parameters.Key, defaultValue)); } } #if NET45Plus if (HttpContext.Current.Items.Contains("_GlimpseWebFormModelBinding")) { parameterModel = (DataBindParameterModel)HttpContext.Current.Items["_GlimpseWebFormModelBinding"]; HttpContext.Current.Items.Remove("_GlimpseWebFormModelBinding"); } #endif if (!DataBindInfo.ContainsKey(DataBoundControl.UniqueID)) { DataBindInfo[DataBoundControl.UniqueID] = new List <DataBindParameterModel>(); } DataBindInfo[DataBoundControl.UniqueID].Add(parameterModel); }
public DataBindingConnection(BaseViewModel viewModel, DataBindInfo dataBindInfo) { this.dataBindInfo = dataBindInfo; this.viewModel = viewModel; _getProperty = ReflectionTool.GetVmPropertyByName(viewModel.GetType(), dataBindInfo.propertyName); if (_getProperty == null) { Debug.LogErrorFormat("get property null {0}:{1}", viewModel.GetType(), dataBindInfo.propertyName); } ReflectionMethodItem item = ReflectionTool.GetComponentMethod(dataBindInfo.component.GetType(), dataBindInfo.invokeFunctionName, _getProperty.PropertyType); if (item == null) { Debug.LogErrorFormat("get invokeMethod null {0}", dataBindInfo.invokeFunctionName); } _invokeMethod = item.methodInfo; foreach (var bindingParameter in dataBindInfo.parameters) { _lstParameter.Add(new BindingParameterInfo(bindingParameter, viewModel)); } ps = new object[dataBindInfo.parameters.Length + 2]; ps[0] = dataBindInfo.component; }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { EditorGUI.BeginProperty(position, label, property); DataBindInfo bindingInfo = property.GetSerializedValue <DataBindInfo>(); string title = bindingInfo.component == null ? "NULL" : bindingInfo.component.ToString(); GUIContent titleContent = new GUIContent(title); if (property.isExpanded) { property.isExpanded = EditorGUI.Foldout(new Rect(position.position - new Vector2(0, 30), position.size), property.isExpanded, titleContent, false); } else { property.isExpanded = EditorGUI.Foldout(position, property.isExpanded, titleContent, true); } if (property.isExpanded) { var amountRect = new Rect(position.x, position.y + 20, position.width, 20); var unitRect = new Rect(position.x, position.y + 40, position.width, 20); var nameRect = new Rect(position.x, position.y + 60, position.width, 20); EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("component"), GUIContent.none); SerializedProperty methodMethod = property.FindPropertyRelative("invokeFunctionName"); List <string> methodList = EditorPropertyCache.GetComponentOpts(bindingInfo.component.GetType()); int index = 0; if (methodList.Count > 0) { index = methodList.IndexOf(methodMethod.stringValue); index = EditorGUI.Popup(unitRect, index, methodList.ToArray()); methodMethod.stringValue = methodList[index]; } SerializedObject o = property.serializedObject; Type tO = o.targetObject.GetType(); PropertyInfo viewModelProperty = tO.GetProperty("ViewModel", BindingFlags.Public | BindingFlags.Instance); string typeModel = viewModelProperty.PropertyType.Name; SerializedProperty propertyProperty = property.FindPropertyRelative("propertyName"); List <string> propertyList = EditorPropertyCache.GetPropertys(typeModel, bindingInfo.component.GetType(), methodMethod.stringValue); if (propertyList.Count > 0) { index = propertyList.IndexOf(propertyProperty.stringValue); index = EditorGUI.Popup(nameRect, index, propertyList.ToArray()); if (index >= 0) { propertyProperty.stringValue = propertyList[index]; } } } EditorGUI.EndProperty(); }
public DataBindingConnection(BaseViewModel viewModel, DataBindInfo dataBindInfo) { this.dataBindInfo = dataBindInfo; this.viewModel = viewModel; _getProperty = viewModel.GetType().GetProperty(dataBindInfo.propertyName, BindingFlags.Instance | BindingFlags.Public); if (_getProperty == null) { Debug.LogErrorFormat("get property null {0}:{1}", viewModel.GetType(), dataBindInfo.propertyName); } _invokeMethod = typeof(UIBindingFunctions).GetMethod(dataBindInfo.invokeFunctionName, BindingFlags.Public | BindingFlags.Static); if (_invokeMethod == null) { Debug.LogErrorFormat("get invokeMethod null {0}", dataBindInfo.invokeFunctionName); } if (_invokeMethod.IsGenericMethod) { _invokeMethod = _invokeMethod.MakeGenericMethod(new Type[] { _getProperty.PropertyType }); } }
ReflectionMethodItem GetMethod(SerializedProperty property) { DataBindInfo bindingInfo = property.GetSerializedValue <DataBindInfo>(); if (bindingInfo == null) { return(null); } Type tO = property.serializedObject.targetObject.GetType(); FieldInfo viewModelProperty = tO.GetField("ViewModel", BindingFlags.NonPublic | BindingFlags.Instance); Type vmType = viewModelProperty.FieldType; PropertyInfo propertyInfo = vmType.GetProperty(bindingInfo.propertyName); if (propertyInfo == null) { return(null); } ReflectionMethodItem item = ReflectionTool.GetComponentMethod(bindingInfo.component.GetType(), bindingInfo.invokeFunctionName, propertyInfo.PropertyType); return(item); }
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { InitErrorStyle(); EditorGUI.BeginProperty(position, label, property); DataBindInfo bindingInfo = property.GetSerializedValue <DataBindInfo>(); if (bindingInfo == null) { return; } string title = bindingInfo.component == null ? "NULL" : bindingInfo.component.ToString(); GUIContent titleContent = new GUIContent(title); bool error = bindingInfo.component == null || string.IsNullOrEmpty(bindingInfo.invokeFunctionName) || string.IsNullOrEmpty(bindingInfo.propertyName); GUIStyle style = error ? errorStyle : EditorStyles.foldout; property.isExpanded = EditorGUI.Foldout(new Rect(position.position, new Vector2(position.width, 20)), property.isExpanded, titleContent, true, style); if (property.isExpanded) { var amountRect = new Rect(position.x, position.y + 20, position.width, 20); var unitRect = new Rect(position.x, position.y + 40, position.width, 20); var nameRect = new Rect(position.x, position.y + 60, position.width, 20); EditorGUI.PropertyField(amountRect, property.FindPropertyRelative("component"), GUIContent.none); SerializedObject o = property.serializedObject; SerializedProperty methodMethod = property.FindPropertyRelative("invokeFunctionName"); lstTemp.Clear(); ReflectionTool.GetComponentMethods(bindingInfo.component.GetType(), lstTemp); int index; if (lstTemp.Count > 0) { index = lstTemp.IndexOf(methodMethod.stringValue); index = Mathf.Max(0, index); index = EditorGUI.Popup(unitRect, index, lstTemp.ToArray()); methodMethod.stringValue = lstTemp[index]; } else { methodMethod.stringValue = ""; } Type tO = o.targetObject.GetType(); FieldInfo viewModelProperty = tO.GetField("ViewModel", BindingFlags.NonPublic | BindingFlags.Instance); Type vmType = viewModelProperty.FieldType; lstTemp.Clear(); ReflectionTool.GetVmPropertysByMethod(vmType, bindingInfo.component.GetType(), methodMethod.stringValue, lstTemp); SerializedProperty propertyProperty = property.FindPropertyRelative("propertyName"); if (lstTemp.Count > 0) { index = lstTemp.IndexOf(propertyProperty.stringValue); index = Mathf.Max(0, index); index = EditorGUI.Popup(nameRect, index, lstTemp.ToArray()); propertyProperty.stringValue = lstTemp[index]; } else { propertyProperty.stringValue = ""; } ReflectionMethodItem item = GetMethod(property); if (item != null && item.parameters.Length > 2) { SerializedProperty parameterProperty = property.FindPropertyRelative("parameters"); parameterProperty.arraySize = item.parameters.Length - 2; for (int i = 0; i < parameterProperty.arraySize; i++) { var rect = new Rect(position.x, position.y + 80 + i * 20, 50, 20); var rectDetail = new Rect(position.x + 50, position.y + 80 + i * 20, position.width - 50, 20); Type parameterType = item.parameters[i + 2]; SerializedProperty parameter = parameterProperty.GetArrayElementAtIndex(i); SerializedProperty dataFromProperty = parameter.FindPropertyRelative("dataFrom"); SerializedProperty paramTypeProperty = parameter.FindPropertyRelative("paramType"); SerializedProperty stringParamProperty = parameter.FindPropertyRelative("paramStr"); paramTypeProperty.stringValue = parameterType.ToString(); dataFromProperty.intValue = EditorGUI.Popup(rect, dataFromProperty.intValue, dataFrom.ToArray()); if (dataFromProperty.intValue == (int)DataFrom.VM) { lstTemp.Clear(); ReflectionTool.GetVmPropertyByType(vmType, parameterType, lstTemp); if (lstTemp.Count > 0) { index = lstTemp.IndexOf(stringParamProperty.stringValue); index = Mathf.Max(0, index); index = EditorGUI.Popup(rectDetail, index, lstTemp.ToArray()); stringParamProperty.stringValue = lstTemp[index]; } } else if (dataFromProperty.intValue == (int)DataFrom.Custom) { if (parameterType == typeof(string)) { stringParamProperty.stringValue = EditorGUI.TextField(rectDetail, stringParamProperty.stringValue); } else { object x = null; try { x = Convert.ChangeType(stringParamProperty.stringValue, parameterType);; } catch (System.Exception) { x = GetDefault(parameterType); } if (parameterType == typeof(int)) { x = EditorGUI.IntField(rectDetail, (int)x); } else if (parameterType == typeof(bool)) { x = EditorGUI.Toggle(rectDetail, (bool)x); } else if (parameterType == typeof(float)) { x = EditorGUI.FloatField(rectDetail, (float)x); } stringParamProperty.stringValue = (string)Convert.ChangeType(x, typeof(string)); } } EditorGUI.PropertyField(rect, dataFromProperty, GUIContent.none); } } o.ApplyModifiedProperties(); } EditorGUI.EndProperty(); }