Ejemplo n.º 1
0
        internal static void FillBoundView(IBoundView view, Context context, IAttributeSet attrs)
        {
            TypedArray attrArr = context.ObtainStyledAttributes(attrs, Resource.Styleable.BoundItem);

            view.Binding           = attrArr.GetString(Resource.Styleable.BoundItem_binding);
            view.VisibilityBinding = attrArr.GetString(Resource.Styleable.BoundItem_visibility_binding);
        }
Ejemplo n.º 2
0
        protected void UpdateProperty(IBoundView view, Dictionary <string, object> cache, Dictionary <string, bool> didChange, List <string> didUpdate)
        {
            if (view.Binding == null)
            {
                return;
            }

            if (!didUpdate.Contains(view.Binding))
            {
                object val = Expressions.Property.Get(_model, _props[view.Binding].Name);//_props[view.Binding].GetValue(_model);
                if (cache.ContainsKey(view.Binding))
                {
                    if (cache[view.Binding] != val)
                    {
                        cache[view.Binding] = val;
                        didChange.Add(view.Binding, true);
                    }
                    else
                    {
                        didChange.Add(view.Binding, false);
                    }
                }
                else
                {
                    cache.Add(view.Binding, val);
                    didChange.Add(view.Binding, true);
                }
                didUpdate.Add(view.Binding);
            }

            if (view.AlwaysUpdate || didChange.ContainsKey(view.Binding))
            {
                view.Apply(cache[view.Binding]);
            }
        }
        public ViewDynamicAccess(IBoundView View)
        {
            view = View;
            var viewType   = View.GetType();
            var fields     = viewType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            var properties =
                viewType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            var delcaredMembers = viewType.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Instance |
                                                      BindingFlags.Public | BindingFlags.NonPublic);

            fieldsDictionary = new Dictionary <string, FieldInfo>();
            foreach (var fieldInfo in fields)
            {
                fieldsDictionary.Add(fieldInfo.Name, fieldInfo);
            }

            propertiesDictionary = new Dictionary <string, PropertyInfo>();
            foreach (var propertyInfo in properties)
            {
                propertiesDictionary.Add(propertyInfo.Name, propertyInfo);
            }

            declaredDictionary = new Dictionary <string, MemberInfo>();
            foreach (var delcaredMember in delcaredMembers)
            {
                declaredDictionary.Add(delcaredMember.Name, delcaredMember);
            }
        }
Ejemplo n.º 4
0
        protected ABoundViewModel(IBoundView View)
        {
            view = View;
            Bind(View);

            var classBinding = GetType().GetCustomAttributes().FirstOrDefault(a => a is ClassBindingAttribute);
            var attribute    = classBinding as ClassBindingAttribute;

            if (attribute != null)
            {
                SetModel(ModelFactory.GetFactory(attribute.ModelType).GetModel());
            }
        }
Ejemplo n.º 5
0
        public void Bind(IBoundView View)
        {
            View.Binder = new ViewDynamicAccess(View);

            var viewModelType = GetType();

            foreach (var memberInfo in viewModelType.GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy))
            {
                foreach (Attribute attribute in memberInfo.GetCustomAttributes(false))
                {
                    var type = attribute.GetType();
                    if (type == typeof(EventBinder))
                    {
                        var eventBinder = attribute as EventBinder;
                        if (eventBinder != null)
                        {
                            dynamic propertyForEvent;
                            if (!View.Binder.GetValue(eventBinder.PropertyToBind, out propertyForEvent))
                            {
                                continue;
                            }

                            var eventInfo = propertyForEvent.GetType()
                                            .GetEvent(eventBinder.EventToBindName ?? eventBinder.EventToBind.ToString());
                            if (eventInfo == null)
                            {
                                continue;
                            }

                            var addHandler = eventInfo.GetAddMethod();
                            var methodInfo = viewModelType.GetMethod(memberInfo.Name,
                                                                     BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

                            if (methodInfo == null)
                            {
                                continue;
                            }
                            var      @delegate      = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, methodInfo);
                            Object[] addHandlerArgs = { @delegate };
                            addHandler.Invoke(propertyForEvent, addHandlerArgs);
                        }
                    }
                }
            }


            foreach (var propertyInfo in viewModelType.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
            {
                foreach (Attribute attribute in propertyInfo.GetCustomAttributes(false))
                {
                    var type     = attribute.GetType();
                    var property = propertyInfo.GetValue(this);

                    if (type == typeof(DataBinder))
                    {
                        if (!(propertyInfo.PropertyType.Name.Contains("BoundProperty") || propertyInfo.PropertyType == typeof(Object)))
                        {
                            continue;
                        }

                        var dataBinder = attribute as DataBinder;
                        if (dataBinder == null)
                        {
                            continue;
                        }

                        if (property == null)
                        {
                            if (propertyInfo.PropertyType == typeof(Object) && dataBinder.GenericType == null)
                            {
                                continue;
                            }

                            var genericType = dataBinder.GenericType == null
                                ? propertyInfo.PropertyType.GetGenericArguments().First()
                                : dataBinder.GenericType;

                            var propertyType = typeof(BoundProperty <>).MakeGenericType(genericType);
                            property = Activator.CreateInstance(propertyType);
                            propertyInfo.SetValue(this, property);
                        }

                        var boundProperty = property as ABoundProperty;

                        if (boundProperty != null)
                        {
                            boundProperty.ViewBinder   = View.Binder;
                            boundProperty.PropertyName = dataBinder.PropertyToBind;
                            boundProperty.CanRead      = CanRead(dataBinder);
                            boundProperty.CanWrite     = CanWrite(dataBinder);
                            boundProperty.OneTime      = dataBinder.Type == DataBindType.OneTime;
                        }
                    }
                    else if (type == typeof(MethodBinder))
                    {
                        var methodBinder = attribute as MethodBinder;
                        if (methodBinder != null)
                        {
                            if (property == null)
                            {
                                if (!(propertyInfo.PropertyType.Name.Contains("BoundMethod") || propertyInfo.PropertyType == typeof(Object)))
                                {
                                    continue;
                                }

                                var genericType = methodBinder.GenericType == null
                                    ? propertyInfo.PropertyType.GetGenericArguments().First()
                                    : methodBinder.GenericType;

                                var propertyType = typeof(BoundMethod <>).MakeGenericType(genericType);
                                property = Activator.CreateInstance(propertyType);
                                propertyInfo.SetValue(this, property);
                            }
                            View.Binder.BindMethod(methodBinder.MethodToBind, property);
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        protected void UpdateVisibility(IBoundView view, Dictionary <string, object> cache, Dictionary <string, bool> didChange, List <string> didUpdate)
        {
            if (view.VisibilityBinding == null)
            {
                return;
            }

            bool   inverted = view.VisibilityBinding.StartsWith("!");
            string bind     = view.VisibilityBinding;

            if (inverted)
            {
                bind = bind.Substring(1);
            }

            if (!didUpdate.Contains(bind))
            {
                object val = Expressions.Property.Get(_model, _props[bind].Name);//_props[bind].GetValue(_model);
                if (cache.ContainsKey(bind))
                {
                    if (cache[bind] != val)
                    {
                        cache[bind] = val;
                        didChange.Add(bind, true);
                    }
                    else
                    {
                        didChange.Add(bind, false);
                    }
                }
                else
                {
                    cache.Add(bind, val);
                    didChange.Add(bind, true);
                }
                didUpdate.Add(bind);
            }

            if (didChange.ContainsKey(bind))
            {
                bool val = (bool)cache[bind];
                if (inverted)
                {
                    if (!val)
                    {
                        view.SetVisibility(ViewStates.Visible);
                    }
                    else
                    {
                        view.SetVisibility(ViewStates.Gone);
                    }
                }
                else
                {
                    if (val)
                    {
                        view.SetVisibility(ViewStates.Visible);
                    }
                    else
                    {
                        view.SetVisibility(ViewStates.Gone);
                    }
                }
            }
        }