码迷,mamicode.com
首页 > 其他好文 > 详细

(MVVM) ListBox Binding

时间:2017-07-15 11:15:42      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:protect   repr   over   text   ext   ons   box   init   insert   

当需要用Lisbbox 来log 一些记录的时候,ObservableCollection 并不可以是记录实时的反应在WPF 的UI上面。

这个时候就需要用一个异步collection 来完成。

    /// <summary>
    /// Represents the asynchronous observable collection.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class AsyncObservableCollection<T> : ObservableCollection<T>
    {
        /// <summary>
        /// The _synchronization context
        /// </summary>
        private readonly SynchronizationContext synchronizationContext = SynchronizationContext.Current;

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncObservableCollection{T}"/> class.
        /// </summary>
        public AsyncObservableCollection()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncObservableCollection{T}"/> class.
        /// </summary>
        /// <param name="list">The list.</param>
        public AsyncObservableCollection(IEnumerable<T> list)
            : base(list)
        {
        }

        /// <summary>
        /// Inserts the item.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="item">The item.</param>
        protected override void InsertItem(int index, T item)
        {
            this.ExecuteOnSyncContext(() => base.InsertItem(index, item));
        }

        /// <summary>
        /// Removes the item.
        /// </summary>
        /// <param name="index">The index.</param>
        protected override void RemoveItem(int index)
        {
            this.ExecuteOnSyncContext(() => base.RemoveItem(index));
        }

        /// <summary>
        /// Sets the item.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="item">The item.</param>
        protected override void SetItem(int index, T item)
        {
            this.ExecuteOnSyncContext(() => base.SetItem(index, item));
        }

        /// <summary>
        /// Moves the item.
        /// </summary>
        /// <param name="oldIndex">The old index.</param>
        /// <param name="newIndex">The new index.</param>
        protected override void MoveItem(int oldIndex, int newIndex)
        {
            this.ExecuteOnSyncContext(() => base.MoveItem(oldIndex, newIndex));
        }

        /// <summary>
        /// Clears the items.
        /// </summary>
        protected override void ClearItems()
        {
            this.ExecuteOnSyncContext(() => base.ClearItems());
        }

        /// <summary>
        /// Executes the on synchronize context.
        /// </summary>
        /// <param name="action">The action.</param>
        private void ExecuteOnSyncContext(Action action)
        {
            if (SynchronizationContext.Current == this.synchronizationContext)
            {
                action();
            }
            else
            {
                this.synchronizationContext.Send(_ => action(), null);
            }
        }
    } 

 另外还需要启用一个新的线程来更新collection

 

(MVVM) ListBox Binding

标签:protect   repr   over   text   ext   ons   box   init   insert   

原文地址:http://www.cnblogs.com/fdyang/p/7181410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!