Пример #1
0
        protected void OnSourcePropertyChanged(object sender, string propertyName)
        {
            Assert.IsTrue(IsBound);

            if (sender != source)
            {
                Debug.LogWarningFormat("Invalid sender {0}:{1}", sender, sender.GetHashCode());
                return;
            }

            if (propertyName != null && sourcePropertyName != propertyName)
            {
                // ignore invalid source path
                return;
            }

            // get collection object
            var collectionObject = BindingUtility.GetPropertyValue(source, sourcePropertyName);

            if (collectionObject == null)
            {
                Debug.LogError("OnPropertyChanged failed, collectionObject is null", factory as UnityEngine.Object);
                return;
            }

            // check if collection object is IEnumerable
            IEnumerable newItemsSource = collectionObject as IEnumerable;

            if (newItemsSource == null)
            {
                Debug.LogError("OnPropertyChanged failed, collectionObject is not IEnumerable", factory as UnityEngine.Object);
                return;
            }

            // check if itemsSource is changed
            if (itemsSource != newItemsSource)
            {
                ChangeItemsSource(newItemsSource);
            }
            else
            {
                // itemsSource is not changed, reset items
                ResetItems();
            }

            UpdateCollectionView();
            UpdateView();
        }
Пример #2
0
        public void Bind(object source)
        {
            if (source == null)
            {
                Debug.LogError("source is null", factory as UnityEngine.Object);
                return;
            }

            if (IsBound)
            {
                Unbind();
            }

            // handle nested property
            var bindingSource = BindingUtility.GetBindingObject(source, sourcePath);

            if (bindingSource == null)
            {
                Debug.LogError("bindingSource is null", factory as UnityEngine.Object);
                return;
            }

            // get collection object
            var collectionObject = BindingUtility.GetPropertyValue(bindingSource, sourcePropertyName);

            if (collectionObject == null)
            {
                Debug.LogError("Binding failed, collectionObject is null", factory as UnityEngine.Object);
                return;
            }

            // check if collection object is IList
            IList collectionSource = collectionObject as IList;

            if (collectionSource == null)
            {
                var msg = string.Format("Binding failed, collection source is not IList object. source={0}, sourcePath={1}, collectionSource={2}",
                                        source, sourcePath, collectionObject.GetType());

                Debug.LogError(msg, factory as UnityEngine.Object);
                return;
            }

            // save source
            this.source      = bindingSource;
            this.itemsSource = collectionSource;

            // register event
            var notifyCollection = itemsSource as INotifyCollectionChanged;

            if (notifyCollection != null)
            {
                notifyCollection.CollectionChanged += OnCollectionChanged;
            }

            // register property event
            var notifyInterface = this.source as INotifyPropertyChanged;

            if (notifyInterface != null)
            {
                notifyInterface.PropertyChanged += OnSourcePropertyChanged;
            }

            ResetItems();

            UpdateView();
        }