Exemplo n.º 1
0
        public VirtualizingObservableCollectionTransformingPagedSource(
            INotifyCollectionChanged sourceCollection,
            Func <TSourceType, TTransformedType> transformItem,
            Func <TTransformedType, TSourceType> transformItemBack)
        {
            sourceCollection.CollectionChanged += this.SourceCollectionOnCollectionChanged;
            this.SourceCollection = sourceCollection as ICollection;

            var collectionAddMethodInfo = sourceCollection.GetType().GetMethod("Add", new[]
            {
                typeof(TSourceType)
            }) ?? sourceCollection.GetType().GetMethod("Add", new[]
            {
                typeof(object)
            });
            var collectionRemoveMethodInfo = sourceCollection.GetType().GetMethod("Remove", new[]
            {
                typeof(TSourceType)
            }) ?? sourceCollection.GetType().GetMethod("Remove", new[]
            {
                typeof(object)
            });

            var collectionClearMethodInfo = sourceCollection.GetType().GetMethod("Clear", new Type[0]);


            if (this.SourceCollection == null || collectionAddMethodInfo == null || collectionRemoveMethodInfo == null || collectionClearMethodInfo == null)
            {
                throw new ArgumentException("SourceCollection not implements ICollection<TSourceType> and/or ICollection", nameof(sourceCollection));
            }

            this.AddToSourceCollection = item =>
            {
                collectionAddMethodInfo.Invoke(this.SourceCollection, new[]
                {
                    item
                });
            };
            this.RemoveFromSourceCollection = item => collectionRemoveMethodInfo.Invoke(this.SourceCollection, new[]
            {
                item
            });

            this.ClearSourceCollection = () => collectionClearMethodInfo.Invoke(this.SourceCollection, new object[0]);

            this.SourceCollectionList = sourceCollection as IList <TSourceType>;
            this.TransformItem        = transformItem;
            this.TransformItemBack    = transformItemBack;
            Task.Factory.StartNew(this.PeriodicalUpdate, TaskCreationOptions.LongRunning);
        }
Exemplo n.º 2
0
        public INotifyPropertyChanged createSubstitute(INotifyCollectionChanged collection)
        {
            var result = new substitute();

            collection.CollectionChanged += onCollectionChange;

            if (collection is IEnumerable enumerable)
            {
                var currentCollection = enumerable.Cast <object>().ToList();
                onCollectionChange(collection, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, currentCollection));
            }
            else
            {
                System.Diagnostics.Debug.WriteLine($"A collection of type '{collection.GetType()}' does not derive from 'IEnumerable' and hence no initial elements were detectable");
            }
            return(result);

            void onCollectionChange(object sender, NotifyCollectionChangedEventArgs e)
            {
                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    int newIndex = e.NewStartingIndex;
                    foreach (object newItem in e.NewItems)
                    {
                        string name = this.GetAttributeName(newItem, newIndex);
                        Contract.Assert(!string.IsNullOrEmpty(name), "The attribute name may not be null or empty");
                        Contract.Assert(!result.Properties.ContainsKey(name), $"The collection already contains a member with name '${name}'");
                        result.Properties[name] = newItem;
                        result.Invoke(result, PropertyMutatedEventArgsExtensions.Create(name, typeof(object), null, newItem));
                        newIndex++;
                    }
                    break;

                case NotifyCollectionChangedAction.Move:
                    break;                             // don't do anything

                case NotifyCollectionChangedAction.Remove:
                    int oldIndex = e.OldStartingIndex;
                    foreach (object newItem in e.OldItems)
                    {
                        string name = this.GetAttributeName(newItem, oldIndex);
                        Contract.Assert(!string.IsNullOrEmpty(name), "The attribute name may not be null or empty");
                        Contract.Assert(result.Properties.ContainsKey(name), $"The collection did not contain a member with name '${name}' to remove");
                        var oldItem = result.Properties[name];
                        result.Properties[name] = null;
                        result.Invoke(result, PropertyMutatedEventArgsExtensions.Create(name, typeof(object), oldItem, null));
                        oldIndex++;
                    }
                    break;

                case NotifyCollectionChangedAction.Replace:
                case NotifyCollectionChangedAction.Reset:
                    throw new NotImplementedException();

                default:
                    throw new ArgumentException();
                }
            }
        }
Exemplo n.º 3
0
        public ObservableSource(INotifyCollectionChanged itRaisesCollectionChanged, string pathElement, PathConnectorTypeEnum pathConnectorType, string binderName)
            : this(pathElement, pathConnectorType, false, binderName)
        {
            SourceKind = SourceKindEnum.CollectionObject;
            Data       = itRaisesCollectionChanged ?? throw new ArgumentNullException($"{nameof(itRaisesCollectionChanged)} was null when constructing Observable Source.");
            Type       = itRaisesCollectionChanged.GetType();

            Status = ObservableSourceStatusEnum.Ready;

            //WeakEventManager<INotifyCollectionChanged, CollectionChangeEventArgs>
            //    .AddHandler(itRaisesCollectionChanged, "CollectionChanged", OnCCEvent);
        }
Exemplo n.º 4
0
        private void ResetCollection(INotifyCollectionChanged collection, IList newCollection)
        {
            if (collection != null)
            {
                var collectionChangedMethod = collection.GetType().GetMethod("OnCollectionChanged", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance);
                if (collectionChangedMethod != null)
                {
                    collectionChangedMethod.Invoke(collection, new object[] { new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, null) });
                    collectionChangedMethod.Invoke(collection, new object[] { new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newCollection) });

                    SetDataContextBinder(DataContextBinder);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Registers the collection and automatically. As soon as the <see cref="INotifyCollectionChanged.CollectionChanged"/> event
        /// occurs, it will automatically create a backup of the collection to support undo.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="tag">The tag.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="collection"/> is <c>null</c>.</exception>
        public void RegisterCollection(INotifyCollectionChanged collection, object tag = null)
        {
            Argument.IsNotNull("collection", collection);

            Log.Debug("Registering collection of type '{0}' with tag '{1}'", collection.GetType().Name, TagHelper.ToString(tag));

            if (!_observers.ContainsKey(collection))
            {
                _observers[collection] = new CollectionObserver(collection, tag, this);

                Log.Debug("Registered collection");
            }
            else
            {
                Log.Debug("Collection already registered, not registered");
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Unregisters the collection and stops automatically watching the collection. All undo/redo history will be removed.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="collection"/> is <c>null</c>.</exception>
        public void UnregisterCollection(INotifyCollectionChanged collection)
        {
            Argument.IsNotNull("collection", collection);

            Log.Debug("Unregistering collection of type '{0}'", collection.GetType().Name);

            ClearActionsForObject(collection);

            if (_observers.ContainsKey(collection))
            {
                _observers[collection].CancelSubscription();
                _observers.Remove(collection);

                Log.Debug("Unregistered collection");
            }
            else
            {
                Log.Debug("Collection was not registered, not unregistered");
            }
        }
Exemplo n.º 7
0
        public CollectionInfo InitiliazeCollection(INotifyCollectionChanged collection, string propertyName)
        {
            if (DBContext == null)
            {
                throw new Exception("DbContext for " + this + " is null, collection can't be initialized");
            }
            if (collection == null)
            {
                throw new Exception("Collection is null");
            }

            CollectionInfo ci;

            ci = Collections.FirstOrDefault(x => x.Collection == collection);
            if (ci != null)
            {
                return(ci);
            }

            ci                = new CollectionInfo();
            ci.Collection     = collection;
            ci.isLoadedFromDB = false;

            // A table and a property of current object
            Type this_type = this.GetType();

            ci.MasterTableInfo = DBContext.DBschema.GetTable(this_type);
            if (ci.MasterTableInfo == null)
            {
                throw new Exception("Can't initialize collection. Master table info not found");
            }
            ci.InversePropertyInfo = ci.MasterTableInfo.GetColumnInfo(propertyName);
            if (ci.InversePropertyInfo == null)
            {
                throw new Exception("Can't initialize collection. Inverse property info not found");
            }

            // The dependent table
            Type typeofcollection           = collection.GetType();
            Type generic_type_of_collection = typeofcollection.GenericTypeArguments[0];

            ci.DependentTableInfo = DBContext.DBschema.GetTable(generic_type_of_collection);
            if (ci.DependentTableInfo == null)
            {
                throw new Exception("Can't initialize collection. Dependent table info not found");
            }

            // The key property
            TypeInfo                 this_type_info   = this_type.GetTypeInfo();
            PropertyInfo             this_prop        = this_type_info.GetDeclaredProperty(propertyName);
            InversePropertyAttribute inverse_property = this_prop.GetCustomAttribute <InversePropertyAttribute>();

            ci.KeyPropertyInfo = ci.DependentTableInfo.GetColumnInfo(inverse_property.PropertyName);
            if (ci.KeyPropertyInfo == null)
            {
                throw new Exception("Can't initialize collection. Key property info not found");
            }

            // The key id property
            TypeInfo            generic_type_of_collection_type_info = generic_type_of_collection.GetTypeInfo();
            PropertyInfo        generic_type_property = generic_type_of_collection_type_info.GetDeclaredProperty(ci.KeyPropertyInfo.ClrName);
            ForeignKeyAttribute foreignKeyAttribute   = generic_type_property.GetCustomAttribute <ForeignKeyAttribute>();

            ci.KeyIdProperytInfo = ci.DependentTableInfo.GetColumnInfo(foreignKeyAttribute.ForeignKeyName);
            if (ci.KeyIdProperytInfo == null)
            {
                throw new Exception("Can't initialize collection. Key id property info not found");
            }

            collection.CollectionChanged += CollectionChanged;
            Collections.Add(ci);

            return(ci);
        }
Exemplo n.º 8
0
		private void ResetCollection(INotifyCollectionChanged collection, IList newCollection)
		{	
			if (collection != null)
			{
				var collectionChangedMethod = collection.GetType().GetMethod("OnCollectionChanged", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance);
				if (collectionChangedMethod != null)
				{
					collectionChangedMethod.Invoke(collection, new object[] { new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset, null) });
					collectionChangedMethod.Invoke(collection, new object[] { new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, newCollection) });

					SetDataContextBinder(DataContextBinder);
				}
			}
		}
Exemplo n.º 9
0
        /// <summary>
        /// Unregisters the collection and stops automatically watching the collection. All undo/redo history will be removed.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="collection"/> is <c>null</c>.</exception>
        public void UnregisterCollection(INotifyCollectionChanged collection)
        {
            Argument.IsNotNull("collection", collection);

            Log.Debug("Unregistering collection of type '{0}'", collection.GetType().Name);

            ClearActionsForObject(collection);

            if (_observers.ContainsKey(collection))
            {
                _observers[collection].CancelSubscription();
                _observers.Remove(collection);

                Log.Debug("Unregistered collection");
            }
            else
            {
                Log.Debug("Collection was not registered, not unregistered");
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Registers the collection and automatically. As soon as the <see cref="INotifyCollectionChanged.CollectionChanged"/> event
        /// occurs, it will automatically create a backup of the collection to support undo.
        /// </summary>
        /// <param name="collection">The collection.</param>
        /// <param name="tag">The tag.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="collection"/> is <c>null</c>.</exception>
        public void RegisterCollection(INotifyCollectionChanged collection, object tag = null)
        {
            Argument.IsNotNull("collection", collection);

            Log.Debug("Registering collection of type '{0}' with tag '{1}'", collection.GetType().Name, TagHelper.ToString(tag));

            if (!_observers.ContainsKey(collection))
            {
                _observers[collection] = new CollectionObserver(collection, tag, this);

                Log.Debug("Registered collection");
            }
            else
            {
                Log.Debug("Collection already registered, not registered");
            }
        }
 public CollectionChangedTrigger(INotifyCollectionChanged source, int tripFilter, IScheduler tripScheduler, Logger logger)
     : base($"{source.GetType().Name}.{nameof(source.CollectionChanged)}", source, tripScheduler, logger)
 {
     _tripFilter = tripFilter;
 }