/// <summary> /// 将InputField包装为事件属性 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="text"></param> /// <returns></returns> public static EventProperty <string> GetEventText(this InputField intput) { var prop = new EventProperty <string>(() => { return(intput.text); }, (x) => { intput.text = x; }, null, intput.onValueChanged); return(prop); }
// <summary> /// 绑定UI /// </summary> public static BaseCommand[] Bind <T>(this InputField text, EventProperty <T> property, Action <InputField, EventProperty <T> > actOnChanged = null, Action <InputField, T> actOnValueChange = null) { if (actOnChanged == null) { actOnChanged = (txt, str) => property.propValue = txt.text.GetValue <T>(); } if (actOnValueChange == null) { actOnValueChange = (txt, str) => txt.text = str == null ? "" : str.ToString(); } BaseCommand[] commands = new BaseCommand[2]; var getListenable = text.onValueChanged.GetListenable(); commands[0] = getListenable.Listen((str) => { actOnChanged(text, property); }, text) .RemoveWhenDestroy(text.gameObject) .WhenMonobehaviorDestory(text.gameObject, (cmd) => { getListenable.RemoveAllListen(); }); var setListenable = property.GetSetListenable(); commands[1] = setListenable.Listen((str) => actOnValueChange(text, str), text) .RemoveWhenDestroy(text.gameObject) .WhenMonobehaviorDestory(text.gameObject, (cmd) => { setListenable.RemoveAllListen(); }); return(commands); }
/// <summary> /// 绑定属性 /// </summary> /// <typeparam name="V"></typeparam> /// <param name="text"></param> /// <param name="property"></param> /// <param name="converter"></param> /// <returns></returns> public static Binding <string, V> Bind <V>(this Text text, EventProperty <V> property, IValueConverter <string, V> converter) { var binding = text.GetEventText().Bind(property, converter); text.gameObject.GetCallbacks().onDestroy += binding.Unbind; return(binding); }
// <summary> /// 绑定UI /// </summary> public static BaseCommand Bind <T>(this Button btn, EventProperty <T> property, Action <Button, T> act) { var setListenable = property.GetSetListenable(); return(setListenable.Listen((str) => act(btn, str), btn) .RemoveWhenDestroy(btn.gameObject) .WhenMonobehaviorDestory(btn.gameObject, (cmd) => { setListenable.RemoveAllListen(); })); }
/// <summary> /// 绑定UI /// </summary> public static BaseCommand Bind <T>(this Image img, EventProperty <T> property, Action <Image, T> actOnPropertyValurChange) { var setListenable = property.GetSetListenable(); return(setListenable.Listen((str) => actOnPropertyValurChange(img, str), img) .RemoveWhenDestroy(img.gameObject) .WhenMonobehaviorDestory(img.gameObject, (cmd) => { setListenable.RemoveAllListen(); })); }
/// <summary> /// 绑定UI /// </summary> /// <typeparam name="T"></typeparam> /// <param name="text"></param> /// <param name="property"></param> /// <param name="act"></param> public static BaseCommand Bind <T>(this Text text, EventProperty <T> property, Action <Text, T> actOnPropertyChange = null) { if (actOnPropertyChange == null) { actOnPropertyChange = (txt, str) => txt.text = str == null ? "" : str.ToString(); } var setListenable = property.GetSetListenable(); return(setListenable.Listen((str) => actOnPropertyChange(text, str), text) .RemoveWhenDestroy(text.gameObject) .WhenMonobehaviorDestory(text.gameObject, (cmd) => { setListenable.RemoveAllListen(); })); }
/// <summary> /// 绑定UI /// </summary> /// <returns></returns> public static Binding <T, V> Bind(EventProperty <T> propertyA, EventProperty <V> propertyB, IValueConverter <T, V> converter) { Binding <T, V> binding = new Binding <T, V>(); binding.propertyA = propertyA; binding.propertyB = propertyB; binding.a2bConverter = converter.Convert; binding.b2aConverter = converter.ConvertBack; propertyA.GetSetListenable().Listen(binding.A2B); propertyB.GetSetListenable().Listen(binding.B2A); return(binding); }
/// <summary> /// 绑定UI /// </summary> /// <returns></returns> public static Binding <T, V> Bind(EventProperty <T> propertyA, EventProperty <V> propertyB, Func <T, V> a2bConverter = null, Func <V, T> b2aConverter = null) { Binding <T, V> binding = new Binding <T, V>(); binding.propertyA = propertyA; binding.propertyB = propertyB; binding.a2bConverter = a2bConverter ?? binding.DefaultA2BConverter; binding.b2aConverter = b2aConverter ?? binding.DefaultB2AConverter; binding.a2bCommand = propertyA.GetSetListenable().Listen(binding.A2B); binding.b2aCommand = propertyB.GetSetListenable().Listen(binding.B2A); return(binding); }
/// <summary> /// 将Text中的text包装为事件属性 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="text"></param> /// <returns></returns> public static EventProperty <string> GetEventText(this Text text) { var prop = new EventProperty <string>(() => { return(text.text); }, (x) => { text.text = x; }); return(prop); }
/// <summary> /// 将两个属性之间进行绑定 /// </summary> /// <typeparam name="V"></typeparam> /// <param name="prop"></param> /// <returns></returns> public Binding <T, V> Bind <V>(EventProperty <V> prop, IValueConverter <T, V> converter) { return(Binding <T, V> .Bind(this, prop, converter)); }
/// <summary> /// 将两个属性之间进行绑定 /// </summary> /// <typeparam name="V"></typeparam> /// <param name="prop"></param> /// <returns></returns> public Binding <T, V> Bind <V>(EventProperty <V> prop) { return(Binding <T, V> .Bind(this, prop)); }