Exemplo n.º 1
0
 public BindEntity(BindableObject bind, string propertyName, BindableObjectHandler handler, Type type)
 {
     this._propertyName = propertyName;
     this._bind         = bind;
     this._handler      = handler;
     this._type         = type;
     setValueAction     = null;
     operation          = BindableObject.BindOperation.Both;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 绑定
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="setter"></param>
        /// <param name="getter"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        public BindableObjectHandler BindProperty <T>(Action <T> setter, Func <T> getter, BindableObject.BindOperation operation = BindableObject.BindOperation.Both)
        {
            Type type = typeof(T);

            if (!_callmap.ContainsKey(type))
            {
                _callmap.Add(type, new Dictionary <string, Action <string, object> >());
            }
            handler = this;
            T tvalue = getter.Invoke();

            handler = null;
            var    lastEntity   = _entitys[_entitys.Count - 1];
            string propertyName = lastEntity.propertyName;

            lastEntity.operation      = operation;
            lastEntity.setValueAction = (name, obj) => {
                T t;
                if (obj == null)
                {
                    t = default(T);
                }
                else
                {
                    t = (T)obj;
                }
                _valuemap.Set <T>(name, t);

                if (setter != null)
                {
                    setter(t);
                }
            };

            _entitys[_entitys.Count - 1] = lastEntity;
            T value = _valuemap.Get <T>(propertyName);

            if (value == null)
            {
                if (!EqualityComparer <T> .Default.Equals(tvalue, default(T)))
                {
                    _entitys[_entitys.Count - 1].setValueAction.Invoke(propertyName, value);
                }
            }
            else
            {
                if (!EqualityComparer <T> .Default.Equals(tvalue, value))
                {
                    _entitys[_entitys.Count - 1].setValueAction.Invoke(propertyName, value);
                }
            }


            //此处更改
            for (int i = 0; i < _entitys.Count; i++)
            {
                if (_entitys[i].type == type && _entitys[i].propertyName == propertyName)
                {
                    _entitys[i].UnBind(false);
                }
            }

            _callmap[type][propertyName] += _entitys[_entitys.Count - 1].setValueAction;

            for (int i = 0; i < _entitys.Count; i++)
            {
                if (_entitys[i].type == type && _entitys[i].propertyName == propertyName)
                {
                    _entitys[i].Bind();
                }
            }
            return(this);
        }