Exemple #1
0
        /// <summary>
        /// 双向/多向/数值同步变化
        /// </summary>
        private void Bind_Test()
        {
            Log.L("——————————————————————————");
            Log.L("多向绑定监听数据变化示例");
            BindableObjectHandler binder = new BindableObjectHandler();
            Binder_A a = new Binder_A();
            Binder_B b = new Binder_B();
            Binder_C c = new Binder_C();

            Log.L("将三个对象的value值 绑定监听");

            binder.BindProperty((value) => { a.value = value; }, () => { return(a.value); });
            binder.BindProperty((value) => { b.value = value; }, () => { return(b.value); });
            binder.BindProperty((value) => { c.value = value; }, () => { return(c.value); });

            Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n");
            Log.L("将a的value值改为1");
            a.value = 1;
            Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n");

            Log.L("将c的value值改为2");
            c.value = 2;
            Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n");

            Log.L("将b解绑,再将ab的值分别改为3、4(按对象解绑)");
            binder.UnBind(b);
            a.value = 3;
            b.value = 4;
            Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n");

            Log.L("将b绑定回去,对a的\"value\"属性进行解绑,再将ab的值分别改为5、6(按对象绑定的属性名解绑)");
            binder.BindProperty((value) => { b.value = value; }, () => { return(b.value); });
            binder.UnBind(a, "value");
            a.value = 5;
            b.value = 6;
            Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n");

            Log.L($"解绑所有value属性,将bc的值改为7、8(按属性名解绑)");
            binder.UnBind("value");
            b.value = 7;
            c.value = 8;
            Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n");

            Log.L($"全部绑定回去,全部解绑,将abc的值分别改为9、10、11(全部解绑)");
            binder.BindProperty((value) => { a.value = value; }, () => { return(a.value); });
            binder.BindProperty((value) => { b.value = value; }, () => { return(b.value); });
            binder.BindProperty((value) => { c.value = value; }, () => { return(c.value); });

            binder.UnBind();

            a.value = 9;
            b.value = 10;
            c.value = 11;
            Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n");
        }
        private void Bind_Test()
        {
            Log.L("init  BindableObject Test");
            BindableObjectHandler binder = new BindableObjectHandler();
            Binder_A a = new Binder_A();
            Binder_B b = new Binder_B();

            Log.L($"a.value = {a.value}\tb.value= {b.value}");
            Log.L("bind");

            binder.BindProperty((value) => { a.value = value; }, () => { return(a.value); })
            .BindProperty((value) => { b.value = value; }, () => { return(b.value); });
            Log.L($"a.value = {a.value}\tb.value= {b.value}");
            Log.L("change value to a");
            a.value = "a";
            Log.L($"a.value = {a.value}\tb.value= {b.value}");


            Log.L("change value to b");
            b.value = "b";
            Log.L($"a.value = {a.value}\tb.value= {b.value}");
        }