public static Action <T> CreateSetDelegate <T>(this PropertyInfo info, object instance)
        {
            Type propertyType      = info.PropertyType;
            var  generatedDelegate = info.GetSetMethod().CreateDelegate(typeof(Action <>).MakeGenericType(propertyType), instance);

            return(data => generatedDelegate.DynamicInvoke(BindingConverter.ConvertBindingValue(propertyType, data)));
        }
Exemplo n.º 2
0
        public static void BindProperty(this VisualTreeElement element, PropertyInfo elementProperty, BindablePropertyAttribute attribute, BindingExpression binding)
        {
            if (!binding.IsDatabound)
            {
                elementProperty.SetValue(element, BindingConverter.ConvertBindingValue(elementProperty.PropertyType, binding.Source));
                return;
            }

            Action <Action> subscriber;
            BindingMode     mode;

            if (string.IsNullOrEmpty(attribute.ChangeHandler))
            {
                subscriber = handler => { };
                mode       = BindingMode.FromViewModel;
            }
            else
            {
                PropertyInfo viewChangeHandler = element.GetType()
                                                 .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                                                 .Single(p => p.Name == attribute.ChangeHandler);
                subscriber = handler =>
                {
                    Action current = (Action)viewChangeHandler.GetValue(element);
                    current += handler;
                    viewChangeHandler.SetValue(element, current);
                };

                mode = BindingMode.TwoWay;
            }

            element.BindingContext.BindProperty(
                subscriber,
                elementProperty.CreateGetDelegate <dynamic>(element),
                elementProperty.CreateSetDelegate <dynamic>(element),
                elementProperty.Name,
                binding.Target,
                mode);
        }