为什么不INotifyCollectionChanged通用? [英] Why isn't INotifyCollectionChanged generic?

查看:224
本文介绍了为什么不INotifyCollectionChanged通用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建一个新的的ObservableCollection< T> Col​​lectionChanged 监听器如下:

If I create a new ObservableCollection<T>, and a CollectionChanged listener as follows:

var c = new ObservableCollection<MyType>();
c.CollectionChanged += new NotifyCollectionChangedEventHandler(h);
...
void h(object sender, NotifyCollectionChangedEventArgs e) 
{
    IList newItems = e.NewItems;
    // non generic IList!  :(
}

为什么不 e.NewItems 的IList&LT; MyType的&GT;

推荐答案

的ObservableCollection 被设计为支持数据绑定方案在诸如WPF平台,其中数据绑定控件不关心对收集的类型,他们一定会。使得通知通用只会使其更难而不给予任何好处写的控制。

The ObservableCollection is designed to support databinding scenarios in platforms like WPF where the databound controls don't care about the type of the collection they're bound to. Making the notifications generic would only make it much harder to write the controls without giving any benefit.

这篇关于为什么不INotifyCollectionChanged通用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆