Exemple #1
0
        /// <summary>
        /// Synchronize with an object controller.
        /// </summary>
        /// <typeparam name="T">Type of element in the collection.</typeparam>
        /// <param name="controller">The controller to synchronize with.</param>
        /// <param name="collectionChangedArgs">Description of what has changed.</param>
        public static void SynchronizeCollection <T>(this NSObjectController controller, NotifyCollectionChangedEventArgs collectionChangedArgs) where T : class
        {
            switch (collectionChangedArgs.Action)
            {
            case NotifyCollectionChangedAction.Add:
                for (int i = 0; i < collectionChangedArgs.NewItems.Count; i++)
                {
                    var item     = collectionChangedArgs.NewItems[i] as T;
                    var nsObject = item as NSObject;
                    if (nsObject != null)
                    {
                        controller.AddObject(nsObject);
                        DebugOutput("SynchronizeCollection: added " + collectionChangedArgs.NewItems[i] + " of type " + collectionChangedArgs.NewItems[i].GetType().FullName);
                        DebugOutput("SynchronizeCollection: item as NSObject is " + nsObject + ", is null? " + (nsObject == null));
                    }
                }
                break;

            case NotifyCollectionChangedAction.Move:
                ErrorReporting.ReportNotImplementedError("ObservableColletionHelpers.SynchronizeCollection:NotifyCollectionChangedAction.Move");
                break;

            case NotifyCollectionChangedAction.Remove:
                for (int i = 0; i < collectionChangedArgs.OldItems.Count; i++)
                {
                    var item     = collectionChangedArgs.OldItems[i] as T;
                    var nsObject = item as NSObject;
                    if (nsObject != null)
                    {
                        controller.RemoveObject(nsObject);
                        DebugOutput("SynchronizeCollection: removed " + collectionChangedArgs.OldItems[i] + " of type " + collectionChangedArgs.OldItems[i].GetType().FullName);
                    }
                }
                break;

            case NotifyCollectionChangedAction.Replace:
                // remove
                for (int i = 0; i < collectionChangedArgs.OldItems.Count; i++)
                {
                    var item     = collectionChangedArgs.OldItems[i] as T;
                    var nsObject = item as NSObject;
                    if (nsObject != null)
                    {
                        controller.RemoveObject(nsObject);
                    }
                }

                // add
                for (int i = 0; i < collectionChangedArgs.NewItems.Count; i++)
                {
                    var item     = collectionChangedArgs.NewItems[i] as T;
                    var nsObject = item as NSObject;
                    if (item != null)
                    {
                        controller.AddObject(nsObject);
                    }
                }
                break;

            case NotifyCollectionChangedAction.Reset:
                ErrorReporting.ReportNotImplementedError("ObservableColletionHelpers.SynchronizeCollection:NotifyCollectionChangedAction.Reset");
                break;

            default:
                break;
            }
        }
Exemple #2
0
        /// <summary>
        /// Offers a simple one-way sync operation to add / remove items to / from a collection via the collection changed event data.
        /// </summary>
        /// <typeparam name="T">The type of elements in the collection.</typeparam>
        /// <param name="targetCollection">The collection to be updated, based upon the collection changed arguments.</param>
        /// <param name="collectionChangedArgs">Event arguments describing how a source collection has changed.</param>
        /// <remarks>Does not support move operations.</remarks>
        /// <exception cref="NotImplementedException">Thrown if the collection changed event arguments describe a move operation.</exception>
        public static void SynchronizeCollection <T>(this ObservableCollection <T> targetCollection, NotifyCollectionChangedEventArgs collectionChangedArgs) where T : class
        {
            switch (collectionChangedArgs.Action)
            {
            case NotifyCollectionChangedAction.Add:
                for (int i = 0; i < collectionChangedArgs.NewItems.Count; i++)
                {
                    var item = collectionChangedArgs.NewItems[i] as T;
                    if (item != null)
                    {
                        targetCollection.Add(item);
                    }
                }
                break;

            case NotifyCollectionChangedAction.Move:
                ErrorReporting.ReportNotImplementedError("ObservableColletionHelpers.SynchronizeCollection:NotifyCollectionChangedAction.Move");
                break;

            case NotifyCollectionChangedAction.Remove:
                for (int i = 0; i < collectionChangedArgs.OldItems.Count; i++)
                {
                    var item = collectionChangedArgs.OldItems[i] as T;
                    if (item != null)
                    {
                        targetCollection.Remove(item);
                    }
                }
                break;

            case NotifyCollectionChangedAction.Replace:
                // remove
                for (int i = 0; i < collectionChangedArgs.OldItems.Count; i++)
                {
                    var item = collectionChangedArgs.OldItems[i] as T;
                    if (item != null)
                    {
                        targetCollection.Remove(item);
                    }
                }

                // add
                for (int i = 0; i < collectionChangedArgs.NewItems.Count; i++)
                {
                    var item = collectionChangedArgs.NewItems[i] as T;
                    if (item != null)
                    {
                        targetCollection.Add(item);
                    }
                }
                break;

            case NotifyCollectionChangedAction.Reset:
                targetCollection.Clear();
                if (collectionChangedArgs.NewItems != null)
                {
                    for (int i = 0; i < collectionChangedArgs.NewItems.Count; i++)
                    {
                        var item = collectionChangedArgs.NewItems[i] as T;
                        if (item != null)
                        {
                            targetCollection.Add(item);
                        }
                    }
                }
                break;

            default:
                break;
            }
        }