示例#1
0
        private void StopObservingProperty(PropertyInfo propertyInfo, EventHandler <ObservablePropertyChangedEventArgs> handler)
        {
            ObservedProperty observedProperty;

            if (_observedProperties.TryGetValue(propertyInfo.Name, out observedProperty))
            {
                bool liveHandlers = false;

                int idx = 0;
                while (idx < observedProperty.Handlers.Length)
                {
                    var scan = observedProperty.Handlers[idx];
                    if (scan != null)
                    {
                        if (scan == handler)
                        {
                            observedProperty.Handlers[idx] = null;
                        }
                        else
                        {
                            liveHandlers = true;
                        }
                    }

                    idx++;
                }

                if (!liveHandlers)
                {
                    _observedProperties = _observedProperties.Remove(propertyInfo.Name);
                }
            }
        }
示例#2
0
        internal void OnPropertyChanged(ModelPropertyChangedEventArgs e)
        {
            if (_propertyChangedHandlers.Count > 0)
            {
                WeakAction <object, ModelPropertyChangedEventArgs>[] handlers = null;
                lock (_lockObject)
                {
                    List <WeakAction <object, ModelPropertyChangedEventArgs> > propertyChangedHandlers;
                    if (_propertyChangedHandlers.TryGetValue(e.Property.Key, out propertyChangedHandlers))
                    {
                        for (int i = propertyChangedHandlers.Count - 1; i >= 0; i--)
                        {
                            if (!propertyChangedHandlers[i].IsAlive)
                            {
                                propertyChangedHandlers.RemoveAt(i);
                            }
                        }

                        if (propertyChangedHandlers.Count == 0)
                        {
                            _propertyChangedHandlers = _propertyChangedHandlers.Remove(e.Property.Key);
                        }
                        else
                        {
                            handlers = propertyChangedHandlers.ToArray();
                        }
                    }
                }

                if (handlers != null)
                {
                    foreach (var handler in handlers)
                    {
                        handler.Invoke(this, e);
                    }
                }
            }

            if (!String.IsNullOrEmpty(e.Property.PropertyName))
            {
                OnPropertyChanged(new PropertyChangedEventArgs(e.Property.PropertyName));
            }
        }
示例#3
0
        /// <summary>
        /// Removes the element with the specified key from the <see cref="T:System.Collections.Generic.IDictionary`2"/>.
        /// </summary>
        /// <returns>
        /// true if the element is successfully removed; otherwise, false.  This method also returns false if <paramref name="key"/> was not found in the original <see cref="T:System.Collections.Generic.IDictionary`2"/>.
        /// </returns>
        /// <param name="key">The key of the element to remove.</param>
        /// <exception cref="T:System.ArgumentNullException"><paramref name="key"/> is null.</exception>
        public bool Remove(TKey key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            int count = _data.Count;

            _data = _data.Remove(key);
            return(_data.Count < count);
        }
示例#4
0
        /// <summary>
        /// Removes an item from the collection.
        /// </summary>
        /// <returns><c>true</c> if the item was removed from the collection, <c>false</c> if it was not found.</returns>
        public bool Remove(T item)
        {
            int oldWeight;

            if (!_items.TryGetValue(item, out oldWeight))
            {
                return(false);
            }

            _totalWeight -= oldWeight;
            _items        = _items.Remove(item);
            return(true);
        }
示例#5
0
        /// <summary>
        /// Sets the value of a <see cref="ModelProperty"/> for this instance.
        /// </summary>
        /// <param name="property">The <see cref="ModelProperty"/> to update.</param>
        /// <param name="value">The new value for the <see cref="ModelProperty"/>.</param>
        public override sealed void SetValue(ModelProperty property, object value)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }
            if (property.DefaultValue is ModelProperty.UnitializedValue)
            {
                throw new ArgumentException("Cannot assign value to derived property: " + property.FullName);
            }

            object currentValue;

            lock (_lockObject)
            {
                object originalValue = GetOriginalValue(property);
                if (!_updatedValues.TryGetValue(property.Key, out currentValue))
                {
                    currentValue = originalValue;
                }

                if (Object.Equals(value, currentValue))
                {
                    return;
                }

                if (Object.Equals(value, originalValue))
                {
                    _updatedValues = _updatedValues.Remove(property.Key);
                }
                else
                {
                    _updatedValues = _updatedValues.AddOrUpdate(property.Key, value);
                }
            }

            OnModelPropertyChanged(new ModelPropertyChangedEventArgs(property, currentValue, value));
        }
示例#6
0
        /// <summary>
        /// Sets the value of a <see cref="ModelProperty"/> for this instance without checking for changes or raising the change-related events.
        /// </summary>
        internal void SetValueCore(ModelProperty property, object value)
        {
            if (!property.IsValueValid(value))
            {
                throw new InvalidCastException("Cannot store " + ((value != null) ? value.GetType().Name : "null") + " in " + property.FullName + " (" + property.PropertyType.Name + ")");
            }

            if (value != property.DefaultValue)
            {
                _values = _values.AddOrUpdate(property.Key, value);
            }
            else
            {
                _values = _values.Remove(property.Key);
            }
        }
        /// <summary>
        /// Gets the metadata for the provided model type.
        /// </summary>
        /// <param name="type">Type of model to get metadata for.</param>
        /// <returns>Requested metadata, <c>null</c> if not found.</returns>
        public ModelMetadata GetModelMetadata(Type type)
        {
            ModelMetadata metadata;

            if (!_repository.TryGetValue(type, out metadata))
            {
                Type metadataType;
                if (_mapping.TryGetValue(type, out metadataType))
                {
                    metadata    = (ModelMetadata)Activator.CreateInstance(metadataType);
                    _mapping    = _mapping.Remove(type);
                    _repository = _repository.AddOrUpdate(type, metadata);
                }
            }
            return(metadata);
        }
示例#8
0
 protected void SetError(string propertyName, string errorMessage)
 {
     if (!String.IsNullOrEmpty(errorMessage))
     {
         _errors = _errors.AddOrUpdate(propertyName, errorMessage);
         IsValid = false;
     }
     else if (_errors.Count > 0)
     {
         _errors = _errors.Remove(propertyName);
         if (_errors.Count == 0)
         {
             IsValid = true;
         }
     }
 }
示例#9
0
        private void AuditHandlers()
        {
            var deadHandlers = new List <string>();

            foreach (var kvp in _handlers)
            {
                var weakAction = kvp.Value;
                if (weakAction != null && !weakAction.IsAlive)
                {
                    deadHandlers.Add(kvp.Key);
                }
            }

            foreach (var key in deadHandlers)
            {
                _handlers = _handlers.Remove(key);
            }
        }
示例#10
0
        /// <summary>
        /// Binds a property on a model to the view model.
        /// </summary>
        /// <param name="viewModelProperty">View model property to bind.</param>
        /// <param name="binding">Information about how the view model property is bound.</param>
        public void SetBinding(ModelProperty viewModelProperty, ModelBinding binding)
        {
            ModelBinding oldBinding;

            if (_bindings.TryGetValue(viewModelProperty.Key, out oldBinding))
            {
                _bindings = _bindings.Remove(viewModelProperty.Key);
                if (!IsObserving(oldBinding.Source, oldBinding.SourceProperty))
                {
                    oldBinding.Source.RemovePropertyChangedHandler(oldBinding.SourceProperty, OnSourcePropertyChanged);
                }
            }
            else if (_selfBindings.TryGetValue(viewModelProperty.Key, out oldBinding))
            {
                _selfBindings = _selfBindings.Remove(viewModelProperty.Key);
            }

            if (binding != null)
            {
                if (ReferenceEquals(binding.Source, this))
                {
                    _selfBindings = _selfBindings.Add(viewModelProperty.Key, binding);
                    RefreshBinding(viewModelProperty.Key, binding);
                }
                else
                {
                    if (!IsObserving(binding.Source, binding.SourceProperty))
                    {
                        binding.Source.AddPropertyChangedHandler(binding.SourceProperty, OnSourcePropertyChanged);
                    }

                    _bindings = _bindings.Add(viewModelProperty.Key, binding);
                    RefreshBinding(viewModelProperty.Key, binding);
                }
            }
        }