Beispiel #1
0
        // <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);
        }
Beispiel #2
0
        // <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(); }));
        }
Beispiel #3
0
        /// <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(); }));
        }
Beispiel #4
0
        /// <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(); }));
        }
Beispiel #5
0
        /// <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);
        }
Beispiel #6
0
        /// <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);
        }