【问题标题】:Why is there no ObservableKeyedCollection<TKey, TValue> in the .NET Framework?为什么 .NET Framework 中没有 ObservableKeyedCollection<TKey, TValue>?
【发布时间】:2010-03-03 08:41:38
【问题描述】:

.NET Framework 从 3.0 版开始包含 ObservableCollection<T>,但为什么没有 ObservableKeyedCollection

好的,我可以通过从 KeyedCollection<TKey,TValue> 派生并实现 INotifyCollectionChanged 接口来实现我自己的集合,但它不是 .NET Framework 的一个很好的补充。

【问题讨论】:

    标签: .net .net-3.0 inotifycollectionchanged


    【解决方案1】:

    没有 ObservableKeyedCollection(或任何其他此类类型,它只是其他泛型类型的组合)的原因是因为ObservableCollection 是泛型的,这使得“ObservableKeyedCollection”的实现变得如此简单:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    
    public class DictionaryWatcher : ObservableCollection<KeyValuePair<string, object>>, IDisposable
    {
        private NotifyCollectionChangedEventHandler watcher;
        private bool watching = false;
    
        public DictionaryWatcher()
        {
            watcher = new NotifyCollectionChangedEventHandler( ReportChange );
            CollectionChanged += watcher;
            Watched = true;
        }
    
        public bool Watched
        {
            get
            {
                return watching;
            }
    
            set
            {
                if (watching)
                {
                    lock (this)
                    {
                        CollectionChanged -= watcher;
                        watching = false;
                    }
                }
            }
        }
    
    public void Dispose()
    {
        Dispose( true );
        GC.SuppressFinalize( this );
    }
    
        public void Initialize()
        {
            this.Add( new KeyValuePair<string, object>( "First", 1 ) );
            this.Add( new KeyValuePair<string, object>( "Second", 2 ) );
            this.Add( new KeyValuePair<string, object>( "Turd", 3 ) );
            KeyValuePair<string, object> badValue = this[2];
            this.Remove( badValue );
        }
    
    protected virtual void Dispose( bool disposing )
    {
        if (disposing && Watched)
        {
            Watched = false;
        }
    }
    
        private void ReportChange( object sender, NotifyCollectionChangedEventArgs e )
        {
            Console.WriteLine( "Change made: {0}", e.Action );
        }
    }
    

    虽然这肯定不是一个单行程序,但大部分都是样板文件。最重要的是,它没有按照您的建议重新实现 ObservableCollection;相反,它充分利用了它。

    它“不是 .NET Framework 的一个很好的补充”的原因是,当已经有一种方法可以做某事时,创建另一种方法来做这件事是个坏主意。完成某些特定任务的方法越少,做得不好的方法就越少。 8)

    工具已经提供,现在就是你如何使用它们了。

    希望有帮助!

    【讨论】:

    • KeyedCollection 中有很多东西不在您的解决方案中。例如基于键的索引器,防止添加具有相同键的项目等。所以上面的代码可能是样板,但它肯定不完整。该框架旨在为我们提供不仅使用方便,而且实现完整的工具...
    • 我同意杰罗恩的观点。这个答案没有 KeyedCollection 的基本功能。我还想指出,如果 ObservableKeyedCollection 是由框架提供的,那么我们将有尽可能少的方法来做到这一点——1——而不是每个人都创建自己的解决方案,其中许多会像这样被破坏。
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      我建议你看看C5。这是一个很棒的通用集合库,它提供可观察集合作为其所有集合的标准,包括AddedInsertedRemovedRemovedAtClearedChanged。此外,C5 系列都支持“编程到接口”的理想。所有接口都提供了底层实现的全部功能——System.Collections.Generic 命名空间中缺少这些功能。此外,还有彻底的documentation。我强烈建议您检查一下。

      【讨论】:

        猜你喜欢
        • 2011-01-16
        • 2014-02-19
        • 1970-01-01
        • 1970-01-01
        • 2012-07-25
        • 1970-01-01
        • 1970-01-01
        • 2016-06-14
        • 2011-10-30
        相关资源
        最近更新 更多