public void UnbindView (View view)
        {
            if (view == null)
                return;

            var tag = view.GetBindingTag ();
            if (tag != null) {
                if (tag.Bindings != null) {
                    foreach (var binding in tag.Bindings) {
                        binding.Dispose();
                    }
                    tag.Bindings.Clear();
                }

                _viewTags.RemoveAll(weak => weak.Target == tag || weak.Target == null);
            }

            var viewGroup = view as ViewGroup;
            if (viewGroup != null) {
                var count = viewGroup.ChildCount;
                for (var i = 0; i < count; i++) {
                    UnbindView(viewGroup.GetChildAt(i));
                }
            }
        }
        /// <summary>
        /// Traverses the view hierarchy and updates the bindings.
        /// </summary>
        protected void BindViewTree(object dataSource, View view, IList<WeakReference> reusableBindings = null) {
            var tag = view.GetBindingTag ();

            if (tag != null) {
                if (!tag.BindingEnabled)
                    return;

                if (tag.OverrideDataSource)
                    dataSource = tag.DataSource;

                if (tag.BindingDescriptions != null) {
                    if (dataSource == null) {
                        MvxBindingTrace.Trace(
                            MvxTraceLevel.Warning,
                            "Binding view {0} with an empty data source.",
                            view.GetType().Name);
                    }

                    if (tag.Bindings.Count > 0 || dataSource != null) {
                        // Try to reuse weak reference for the tag:
                        WeakReference weakTag = null;
                        if (reusableBindings != null) {
                            for (var i = 0; i < reusableBindings.Count; i++) {
                                if (reusableBindings[i] == null)
                                    continue;
                                if (reusableBindings[i].Target == tag) {
                                    weakTag = reusableBindings[i];
                                    reusableBindings[i] = null;
                                }
                            }
                        }
                        if (weakTag == null)
                            weakTag = new WeakReference(tag);
                        _viewTags.Add(weakTag);

                        if (tag.Bindings.Count > 0) {
                            // Update old bindings only:
                            foreach (var binding in tag.Bindings) {
                                binding.DataContext = dataSource;
                            }
                        } else {
                            // Create bindings:
                            var bindings = this.GetService<IMvxBinder>()
                                .Bind(dataSource, view, tag.BindingDescriptions);
                            if (bindings != null) {
                                foreach (var binding in bindings) {
                                    tag.Bindings.Add(binding);
                                }
                            }
                        }
                    }
                }
            }

            var viewGroup = view as ViewGroup;
            if (viewGroup != null) {
                var count = viewGroup.ChildCount;
                for (var i = 0; i < count; i++) {
                    BindViewTree(dataSource, viewGroup.GetChildAt(i), reusableBindings);
                }
            }
        }
Beispiel #3
0
        public void Attach (View view, IAttributeSet attrs)
        {
            var tag = ParseAttributes (view.Context, attrs);
            if (tag == null)
                return;

            if (view.GetBindingTag () != null) {
                MvxBindingTrace.Trace (MvxTraceLevel.Warning,
                    "Trying to attach binding information to already configured view. Using binding elements on <fragment> tag?");
                return;
            }

            view.SetBindingTag (tag);
        }