获取 ICollectionView 的计数 属性

Get Count property of ICollectionView

我有 ICollectionView

private ICollectionView _snapshotList;

    public ICollectionView SnapshotList {
        get { return _snapshotList; }
    }

我在 ViewModel 构造函数中设置,其中 this.smapshotListModel.SnapshotItems returns ObservableCollection

_snapshotList = CollectionViewSource.GetDefaultView(this.snapshotListModel.SnapshotItems);
        _snapshotList.Filter = ErrorMsgFilter;
        _snapshotList.CollectionChanged += OnCollectionChanged;

稍后在 collectionChanged 事件中我试图获取此集合中的项目数,

        void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
        this.ItemCount = _snapshotList.Count();
    }

但是它抛出错误,没有 Count 方法的定义。

这样试试;

_snapshotList.Cast<object>().Count();

ICollectionView 实现了 IEnumarable 但它没有像 List<TSource>HashSet<TSource> 等那样实现 IEnumerable<TSource>。所以,ICollectionView 没有没有通用类型,我们必须将它转换为 IEnumerable<object> 一次以启用 Count() 方法。