Exemplo n.º 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="collection">The collection to copy into the controller.</param>
        /// <remarks>TODO / BUG: Blindly adds everything! Should confirm what's already in <paramref name="controller"/> and remove what's not!</remarks>
        public static void SynchronizeCollection <T>(this NSObjectController controller, ObservableCollection <T> collection)
        {
            var selectAddedObjects = false;
            var arrayController    = controller as NSArrayController;

            if (arrayController != null)
            {
                selectAddedObjects = arrayController.SelectsInsertedObjects;
                arrayController.SelectsInsertedObjects = false;
            }
            foreach (var item in collection.OfType <NSObject>())
            {
                controller.AddObject(item);
            }
            if (arrayController != null)
            {
                arrayController.SelectsInsertedObjects = selectAddedObjects;
            }
        }
Exemplo n.º 2
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;
            }
        }