/// <summary>
        /// Initializes a new instance of the <see cref="ObservableCollectionChangedEventArgs{T}"/> class.
        /// </summary>
        /// <param name="observableCollectionChange">The observable collection change.</param>
        /// <exception cref="System.ArgumentNullException"></exception>
        public ObservableCollectionChangedEventArgs(IObservableCollectionChange <T> observableCollectionChange)
        {
            if (observableCollectionChange == null)
            {
                throw new ArgumentNullException(nameof(observableCollectionChange));
            }

            Change = observableCollectionChange;
        }
예제 #2
0
        /// <summary>
        ///     Notifies all <see cref="CollectionChanges" /> and <see cref="Resets" /> subscribers and
        ///     raises the (observable)collection changed events.
        /// </summary>
        /// <param name="observableCollectionChange">The observable collection change.</param>
        protected virtual void NotifyObserversAboutCollectionChanges(IObservableCollectionChange <T> observableCollectionChange)
        {
            if (observableCollectionChange == null)
            {
                throw new ArgumentNullException(nameof(observableCollectionChange));
            }

            CheckForAndThrowIfDisposed();

            // go ahead and check whether a Reset or item add, -change, -move or -remove shall be signaled
            // .. based on the ThresholdAmountWhenChangesAreNotifiedAsReset value
            var actualObservableCollectionChange =
                (observableCollectionChange.ChangeType == ObservableCollectionChangeType.Reset ||
                 IsItemsChangedAmountGreaterThanResetThreshold(1, ThresholdAmountWhenChangesAreNotifiedAsReset))
                    ? ObservableCollectionChange <T> .Reset
                    : observableCollectionChange;

            // raise events and notify about collection changes

            if (actualObservableCollectionChange.ChangeType == ObservableCollectionChangeType.ItemAdded ||
                actualObservableCollectionChange.ChangeType == ObservableCollectionChangeType.ItemRemoved ||
                actualObservableCollectionChange.ChangeType == ObservableCollectionChangeType.Reset)
            {
                try
                {
                    CountChangesObserver.OnNext(Count);
                }
                catch (Exception exception)
                {
                    var observerException = new ObserverException(
                        $"An error occured notifying {nameof(CountChanges)} Observers of this {this.GetType().Name}.",
                        exception);

                    ObserverExceptionsObserver.OnNext(observerException);

                    if (observerException.Handled == false)
                    {
                        throw;
                    }
                }
            }

            try
            {
                CollectionChangesObserver.OnNext(actualObservableCollectionChange);
            }
            catch (Exception exception)
            {
                var observerException = new ObserverException(
                    $"An error occured notifying {nameof(CollectionChanges)} Observers of this {this.GetType().Name}.",
                    exception);

                ObserverExceptionsObserver.OnNext(observerException);

                if (observerException.Handled == false)
                {
                    throw;
                }
            }

            try
            {
                RaiseCollectionChanged(actualObservableCollectionChange.ToNotifyCollectionChangedEventArgs());
            }
            catch (Exception exception)
            {
                var observerException = new ObserverException(
                    $"An error occured notifying CollectionChanged Subscribers of this {this.GetType().Name}.",
                    exception);

                ObserverExceptionsObserver.OnNext(observerException);

                if (observerException.Handled == false)
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// Converts the given <paramref name="observableCollectionChange"/> to its <see cref="NotifyCollectionChangedEventArgs"/> counter part.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="observableCollectionChange">The <see cref="IObservableCollectionChange{T}"/> instance containing the event data.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static NotifyCollectionChangedEventArgs ToNotifyCollectionChangedEventArgs <T>(this IObservableCollectionChange <T> observableCollectionChange)
        {
            if (observableCollectionChange == null)
            {
                throw new ArgumentNullException(nameof(observableCollectionChange));
            }

            switch (observableCollectionChange.ChangeType)
            {
            case ObservableCollectionChangeType.ItemAdded:
                return(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, observableCollectionChange.Item));

            case ObservableCollectionChangeType.ItemChanged:
                return(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, observableCollectionChange.Item, observableCollectionChange.Item));

            case ObservableCollectionChangeType.ItemRemoved:
                return(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, observableCollectionChange.Item));

            case ObservableCollectionChangeType.Reset:
                return(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

            default:
                throw new ArgumentOutOfRangeException(nameof(observableCollectionChange), "This should not happen.");
            }
        }