상속: Behavior
예제 #1
0
        private void SetupBindings(Element element, object model)
        {
            Debug.Assert(element != null);

            string bindings = (string)element.GetAttribute(Application.BindingsAttribute);
            bindings.ReplaceRegex(Application.BindingsRegex, delegate(string match /*, string binderType, string expressionType, string expressionValue */) {
                string binderType = (string)Arguments.GetArgument(1);
                string expressionType = (string)Arguments.GetArgument(2);
                string expressionValue = (string)Arguments.GetArgument(3);

                ExpressionFactory expressionFactory = _registeredExpressions[expressionType];
                Debug.Assert(expressionFactory != null, "Unknown expression of type '" + expressionType + "' found.");

                if (expressionFactory != null) {
                    Expression expression = expressionFactory(model, expressionType, expressionValue);
                    Binder binder = null;

                    // TODO: Add support for binding attributes - @xxx

                    if (binderType.StartsWith("on.")) {
                        Debug.Assert(expression.CanChange == false, "Events cannot be bound to dynamic expressions.");
                        Debug.Assert(expression.GetValue() is Action);

                        binder = new EventBinder(element, binderType.Substr(3), (ElementEventListener)expression.GetValue());
                    }
                    else if (binderType.StartsWith("style.")) {
                        object style = element.Style;
                        binder = new PropertyBinder(style, binderType.Substr(6), expression);
                    }
                    else {
                        BinderFactory binderFactory = _registeredBinders[binderType];
                        if (binderFactory == null) {
                            binder = new PropertyBinder(element, binderType, expression);
                        }
                        else {
                            binder = binderFactory(element, binderType, expression);
                        }
                    }

                    if (binder != null) {
                        binder.Update();
                        if (expression.CanChange == false) {
                            // Since the expression value cannot change, there isn't a whole lot of need
                            // to keep the binder alive and manage it.
                            binder = null;
                        }
                    }

                    if (binder != null) {
                        // The binder is managed using a behavior that is attached to the element.
                        // This allows stashing the model for later retrieval, as well as a way to
                        // dispose bindings (the behavior disposes all binders it is managing).

                        BinderManager binderManager = (BinderManager)Behavior.GetBehavior(element, typeof(BinderManager));
                        if (binderManager == null) {
                            binderManager = new BinderManager();
                            binderManager.Initialize(element, null);
                            binderManager.Model = model;
                        }

                        binderManager.AddBinder(binder);
                    }
                }

                return String.Empty;
            });
        }
예제 #2
0
        private void SetupBindings(Element element, object model)
        {
            Debug.Assert(element != null);

            string bindings = (string)element.GetAttribute(Application.BindingsAttribute);

            bindings.ReplaceRegex(Application.BindingsRegex, delegate(string match /*, string binderType, string expressionType, string expressionValue */) {
                string binderType      = (string)Arguments.GetArgument(1);
                string expressionType  = (string)Arguments.GetArgument(2);
                string expressionValue = (string)Arguments.GetArgument(3);

                ExpressionFactory expressionFactory = _registeredExpressions[expressionType];
                Debug.Assert(expressionFactory != null, "Unknown expression of type '" + expressionType + "' found.");

                if (expressionFactory != null)
                {
                    Expression expression = expressionFactory(model, expressionType, expressionValue);
                    Binder binder         = null;

                    // TODO: Add support for binding attributes - @xxx

                    if (binderType.StartsWith("on."))
                    {
                        Debug.Assert(expression.CanChange == false, "Events cannot be bound to dynamic expressions.");
                        Debug.Assert(expression.GetValue() is Action);

                        binder = new EventBinder(element, binderType.Substr(3), (ElementEventListener)expression.GetValue());
                    }
                    else if (binderType.StartsWith("style."))
                    {
                        object style = element.Style;
                        binder       = new PropertyBinder(style, binderType.Substr(6), expression);
                    }
                    else
                    {
                        BinderFactory binderFactory = _registeredBinders[binderType];
                        if (binderFactory == null)
                        {
                            binder = new PropertyBinder(element, binderType, expression);
                        }
                        else
                        {
                            binder = binderFactory(element, binderType, expression);
                        }
                    }

                    if (binder != null)
                    {
                        binder.Update();
                        if (expression.CanChange == false)
                        {
                            // Since the expression value cannot change, there isn't a whole lot of need
                            // to keep the binder alive and manage it.
                            binder = null;
                        }
                    }

                    if (binder != null)
                    {
                        // The binder is managed using a behavior that is attached to the element.
                        // This allows stashing the model for later retrieval, as well as a way to
                        // dispose bindings (the behavior disposes all binders it is managing).

                        BinderManager binderManager = (BinderManager)Behavior.GetBehavior(element, typeof(BinderManager));
                        if (binderManager == null)
                        {
                            binderManager = new BinderManager();
                            binderManager.Initialize(element, null);
                            binderManager.Model = model;
                        }

                        binderManager.AddBinder(binder);
                    }
                }

                return(String.Empty);
            });
        }