示例#1
0
        private void Observable_Test()
        {
            Log.L("init ObservableObject Test");
            ObservableObjectHandler binder = new ObservableObjectHandler();
            Observable_A            a      = new Observable_A();

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

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

            Log.L($"a.value = {a.value}");

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


            Log.L("change value to b");
            a.value = "b";
            Log.L($"a.value = {a.value}");
        }
示例#2
0
        /// <summary>
        /// 单向/数据变化监听
        /// </summary>
        private void Observable_Test()
        {
            Log.L("——————————————————————————");
            Log.L("单向绑定监听数据变化示例");
            Log.L("需要新建一个ObservableObjectHandler对象用于对继承了ObservableObject类的对象变化进行监听");
            Log.L("新建对象binder和被观察的对象observedA");
            ObservableObjectHandler binder    = new ObservableObjectHandler();
            Observable_A            observedA = new Observable_A();

            Log.L($"observedA当前的value值为{observedA.value} \n");

            Log.L("开始绑定操作,参数getter和setter将会被调用一次");

            binder.BindProperty(
                (value) =>
            {
                observedA.value = value;
                Log.L($"值被改变,新值为{value}");
            },
                () =>
            {
                return(observedA.value);
            }
                );

            Log.L($"observedA当前的value值为{observedA.value} \n");

            Log.L("将observedA的value值改为1");
            observedA.value = 1;
            Log.L($"observedA当前的value值为{observedA.value} \n");


            Log.L("将observedA的value值改为2");
            observedA.value = 2;
            Log.L($"observedA当前的value值为{observedA.value} \n");
        }