Exemplo n.º 1
0
        private bool EvalBindingHooks <TViewModel, TView>(TViewModel viewModel, TView view, Expression vmExpression, Expression viewExpression, BindingDirection direction)
            where TViewModel : class
        {
            var hooks = Locator.Current.GetServices <IPropertyBindingHook>();

            var vmFetcher = default(Func <IObservedChange <object, object>[]>);

            if (vmExpression != null)
            {
                vmFetcher = () =>
                {
                    IObservedChange <object, object>[] fetchedValues;
                    Reflection.TryGetAllValuesForPropertyChain(out fetchedValues, viewModel, vmExpression.GetExpressionChain());
                    return(fetchedValues);
                };
            }
            else
            {
                vmFetcher = () =>
                {
                    return(new[]
                    {
                        new ObservedChange <object, object>(null, null, viewModel)
                    });
                };
            }

            var vFetcher = new Func <IObservedChange <object, object>[]>(() =>
            {
                IObservedChange <object, object>[] fetchedValues;
                Reflection.TryGetAllValuesForPropertyChain(out fetchedValues, view, viewExpression.GetExpressionChain());
                return(fetchedValues);
            });

            var shouldBind = hooks.Aggregate(true, (acc, x) =>
                                             acc && x.ExecuteHook(viewModel, view, vmFetcher, vFetcher, direction));

            if (!shouldBind)
            {
                var vmString = $"{typeof(TViewModel).Name}.{string.Join(".", vmExpression)}";
                var vString  = $"{typeof(TView).Name}.{string.Join(".", viewExpression)}";
                this.Log().Warn("Binding hook asked to disable binding {0} => {1}", vmString, vString);
            }

            return(shouldBind);
        }