コード例 #1
0
 /// <summary>
 /// Invokes an action for each item in a collection and subsequently each item added or
 /// removed from the collection.
 /// </summary>
 /// <typeparam name="T">The type of the collection items.</typeparam>
 /// <param name="collection">The collection.</param>
 /// <param name="added">
 /// An action called initially for each item in the collection and subsequently for each
 /// item added to the collection. The parameters passed are the index in the collection and
 /// the item.
 /// </param>
 /// <param name="removed">
 /// An action called for each item removed from the collection. The parameters passed are
 /// the index in the collection and the item.
 /// </param>
 /// <param name="reset">
 /// An action called when the collection is reset.
 /// </param>
 /// <returns>A disposable used to terminate the subscription.</returns>
 public static IDisposable ForEachItem <T>(
     this IAvaloniaReadOnlyList <T> collection,
     Action <T> added,
     Action <T> removed,
     Action reset)
 {
     return(collection.ForEachItem((_, i) => added(i), (_, i) => removed(i), reset));
 }
コード例 #2
0
 /// <summary>
 /// Invokes an action for each item in a collection and subsequently each item added or
 /// removed from the collection.
 /// </summary>
 /// <typeparam name="T">The type of the collection items.</typeparam>
 /// <param name="collection">The collection.</param>
 /// <param name="added">
 /// An action called initially for each item in the collection and subsequently for each
 /// item added to the collection. The parameters passed are the index in the collection and
 /// the item.
 /// </param>
 /// <param name="removed">
 /// An action called for each item removed from the collection. The parameters passed are
 /// the index in the collection and the item.
 /// </param>
 /// <param name="reset">
 /// An action called when the collection is reset.
 /// </param>
 /// <param name="weakSubscription">
 /// Indicates if a weak subscription should be used to track changes to the collection.
 /// </param>
 /// <returns>A disposable used to terminate the subscription.</returns>
 public static IDisposable ForEachItem <T>(
     this IAvaloniaReadOnlyList <T> collection,
     Action <T> added,
     Action <T> removed,
     Action reset,
     bool weakSubscription = false)
 {
     return(collection.ForEachItem((_, i) => added(i), (_, i) => removed(i), reset, weakSubscription));
 }
コード例 #3
0
        public static IAvaloniaReadOnlyList <TDerived> CreateDerivedList <TSource, TDerived>(
            this IAvaloniaReadOnlyList <TSource> collection,
            Func <TSource, TDerived> select)
        {
            var result = new AvaloniaList <TDerived>();

            collection.ForEachItem(
                (i, item) => result.Insert(i, select(item)),
                (i, item) => result.RemoveAt(i),
                () => result.Clear());

            return(result);
        }
コード例 #4
0
        /// <summary>
        /// Listens for property changed events from all items in a collection.
        /// </summary>
        /// <typeparam name="T">The type of the collection items.</typeparam>
        /// <param name="collection">The collection.</param>
        /// <param name="callback">A callback to call for each property changed event.</param>
        /// <returns>A disposable used to terminate the subscription.</returns>
        public static IDisposable TrackItemPropertyChanged <T>(
            this IAvaloniaReadOnlyList <T> collection,
            Action <Tuple <object, PropertyChangedEventArgs> > callback)
        {
            List <INotifyPropertyChanged> tracked = new List <INotifyPropertyChanged>();

            PropertyChangedEventHandler handler = (s, e) =>
            {
                callback(Tuple.Create(s, e));
            };

            collection.ForEachItem(
                x =>
            {
                var inpc = x as INotifyPropertyChanged;

                if (inpc != null)
                {
                    inpc.PropertyChanged += handler;
                    tracked.Add(inpc);
                }
            },
                x =>
            {
                var inpc = x as INotifyPropertyChanged;

                if (inpc != null)
                {
                    inpc.PropertyChanged -= handler;
                    tracked.Remove(inpc);
                }
            },
                null);

            return(Disposable.Create(() =>
            {
                foreach (var i in tracked)
                {
                    i.PropertyChanged -= handler;
                }
            }));
        }