private void DoPropertyUnBind() { if (this.Source != null && this.SetValueHandler != null) { this.Source.RemoveValueChangedListener(this.SetValueHandler); this.SetValueHandler = null; } if (this.ValueChangedHandler != null) { Type cptType = this.Component.GetType(); if (Toggle_Type.IsAssignableFrom(cptType) && this.Property.Equals("isOn")) { (this.Component as Toggle).onValueChanged.RemoveListener((UnityAction <bool>) this.ValueChangedHandler); } if (Input_Type.IsAssignableFrom(cptType) && this.Property.Equals("text")) { (this.Component as InputField).onValueChanged.RemoveListener((UnityAction <string>) this.ValueChangedHandler); } if (Dropdown_Type.IsAssignableFrom(cptType) && this.Property.Equals("value")) { (this.Component as Dropdown).onValueChanged.RemoveListener((UnityAction <int>) this.ValueChangedHandler); } if (Slider_Type.IsAssignableFrom(cptType) && this.Property.Equals("value")) { (this.Component as Slider).onValueChanged.RemoveListener((UnityAction <float>) this.ValueChangedHandler); } this.ValueChangedHandler = null; } }
private void DoAnimatorBind(VMBehaviour vm, GameObject obj) { Animator cptAnim = obj.GetComponent <Animator>(); if (cptAnim == null) { Debugger.LogError("DataBinder", obj.name + " Animator null."); return; } Type propertyType = typeof(string); if (!CheckDataTypeValid(propertyType, obj)) { return; } this.SetValueHandler = delegate(IData source) { object value = this.Source.FastGetValue(); if (this.Converter != null) { value = this.Converter.Convert(value, propertyType, this.Definer.ConverterParameter, vm); } cptAnim.StopPlayback(); string animName = (string)value; if (!string.IsNullOrEmpty(animName)) { cptAnim.Play(animName, this.AnimatorLayer); } }; this.Source.AddValueChangedListener(this.SetValueHandler); this.SetValueHandler.Invoke(this.Source); }
public void Detach(DataChangedHandler handler) { _valueChanged -= handler; if (_valueChanged == null) { _prop.Detach(OnPropertyChanged); _attachedToProperty = false; } }
public void Attach(DataChangedHandler handler) { _valueChanged += handler; if (!_attachedToProperty) { _prop.Attach(OnPropertyChanged); _attachedToProperty = true; } }
private void DoActiveBind(VMBehaviour vm, GameObject obj) { Type propertyType = typeof(bool); if (!CheckDataTypeValid(propertyType, obj)) { return; } this.SetValueHandler = delegate(IData source) { object value = this.Source.FastGetValue(); if (this.Converter != null) { value = this.Converter.Convert(value, propertyType, this.Definer.ConverterParameter, vm); } obj.SetActive((bool)value); }; this.Source.AddValueChangedListener(this.SetValueHandler); this.SetValueHandler.Invoke(this.Source); }
private void DoBind() { if (!this.isActiveAndEnabled) { return; } if (this.Template == null || this.sourceData == null) { return; } if (this.obj != null) { Destroy(this.obj); } this.obj = GameObject.Instantiate(this.Template, this.transform); this.obj.SetActive(true); this.objVM = this.obj.GetComponent <VMBehaviour>(); this.handlers.Clear(); foreach (KeyValuePair <string, IData> kv in this.sourceData.Fields) { IData dstData = this.objVM.GetData(kv.Key); IData srcData = this.sourceData[kv.Key]; if (dstData != null) // && srcData.GetType() == dstData.GetType()) // CopyFrom 本身做了数据检查 { DataChangedHandler handler = delegate(IData src) { dstData.CopyFrom(src); }; handler.Invoke(srcData); srcData.AddValueChangedListener(handler); this.handlers[srcData] = handler; } } }
public void Detach(DataChangedHandler handler) { // Not supported }
public void Detach(DataChangedHandler handler) { _valueChanged -= handler; }
public JournalViewModel(Journal model) : base(model, () => model.JournalID) { DataChanged += new DataChangedHandler(JournalViewModel_DataChanged); }
public AssociateViewModel(Associate model) : base(model, () => model.AssociateID) { DataChanged += new DataChangedHandler(AssociateViewModel_DataChanged); }
private void OnUpdateElement(ChangeType type, string elementName, object value) { DataChangedHandler handler = UpdateElement; handler?.Invoke(this, new DataChangedArgs(type, elementName, value)); }
private void DoPropertyBind(VMBehaviour vm, GameObject obj) { if (this.Component == null) { Debugger.LogError("DataBinder", obj.name + " component null."); return; } if (string.IsNullOrEmpty(this.Property)) { Debugger.LogError("DataBinder", obj.name + " property empty."); return; } if (componentType == null) { componentType = this.Component.GetType(); componentReflection = ReflectionCache.Singleton[componentType]; } PropertyInfo propertyInfo = componentReflection.GetProperty(this.Property); if (propertyInfo == null || propertyInfo.GetSetMethod() == null) { Debugger.LogError("DataBinder", obj.name + " property null or not support."); return; } Type propertyType = propertyInfo.PropertyType; if (!CheckDataTypeValid(propertyType, obj)) { return; } ISetValue propertySetter = SetterWrapper.CreatePropertySetterWrapper(propertyInfo); if (propertySetter == null) { return; } // 数据单向绑定 this.SetValueHandler = delegate(IData source) { object value = this.Source.FastGetValue(); if (this.Converter != null) { value = this.Converter.Convert(value, propertyType, this.Definer.ConverterParameter, vm); } propertySetter.Set(this.Component, value); // ToggleGroup 特殊处理 if (Toggle_Type.IsAssignableFrom(componentType) && this.Property.Equals("isOn")) { Toggle t = this.Component as Toggle; if (t.group != null && t.isOn) { try { t.group.NotifyToggleOn(t); } catch (System.Exception) { } } } }; this.Source.AddValueChangedListener(this.SetValueHandler); this.SetValueHandler.Invoke(this.Source); // 可交互组件的双向绑定 if (Toggle_Type.IsAssignableFrom(componentType) && this.Property.Equals("isOn")) { this.ValueChangedHandler = new UnityAction <bool>(delegate(bool arg) { if (this.Converter != null) { object value = this.Converter.ConvertBack(arg, this.Source.GetBindDataType(), this.Definer.ConverterParameter, vm); this.Source.FastSetValue(value); } else { (this.Source as BaseData <bool>).Set(arg); } }); (this.Component as Toggle).onValueChanged.AddListener((UnityAction <bool>) this.ValueChangedHandler); } if (Input_Type.IsAssignableFrom(componentType) && this.Property.Equals("text")) { this.ValueChangedHandler = new UnityAction <string>(delegate(string arg) { if (this.Converter != null) { object value = this.Converter.ConvertBack(arg, this.Source.GetBindDataType(), this.Definer.ConverterParameter, vm); this.Source.FastSetValue(value); } else { (this.Source as BaseData <string>).Set(arg); } }); (this.Component as InputField).onValueChanged.AddListener((UnityAction <string>) this.ValueChangedHandler); } if (Dropdown_Type.IsAssignableFrom(componentType) && this.Property.Equals("value")) { this.ValueChangedHandler = new UnityAction <int>(delegate(int arg) { if (this.Converter != null) { object value = this.Converter.ConvertBack(arg, this.Source.GetBindDataType(), this.Definer.ConverterParameter, vm); this.Source.FastSetValue(value); } else { (this.Source as BaseData <int>).Set(arg); } }); (this.Component as Dropdown).onValueChanged.AddListener((UnityAction <int>) this.ValueChangedHandler); } if (Slider_Type.IsAssignableFrom(componentType) && this.Property.Equals("value")) { this.ValueChangedHandler = new UnityAction <float>(delegate(float arg) { if (this.Converter != null) { object value = this.Converter.ConvertBack(arg, this.Source.GetBindDataType(), this.Definer.ConverterParameter, vm); this.Source.FastSetValue(value); } else { (this.Source as BaseData <float>).Set(arg); } }); (this.Component as Slider).onValueChanged.AddListener((UnityAction <float>) this.ValueChangedHandler); } }
public void RemoveValueChangedListener(DataChangedHandler handler) { valueChangedHandlers.Remove(handler); }
public void AddValueChangedListener(DataChangedHandler handler) { valueChangedHandlers.Add(handler); }
public void Attach(DataChangedHandler handler) { _valueChanged += handler; }
public ViewModelBase() { DataChanged += new DataChangedHandler(ViewModelBase_DataChanged); }