可观察的堆栈和队列
我正在寻找 Stack
和 Queue
的 INotifyCollectionChanged
实现。我可以自己动手,但我不想重新发明轮子。
I'm looking for an INotifyCollectionChanged
implementation of Stack
and Queue
. I could roll my own but I don't want to reinvent the wheel.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了同样的问题,想与其他人分享我的解决方案。希望这对某人有帮助。
I run into the same issue and want to share my solution to others. Hope this is helpful to someone.
使用堆栈和队列(几乎根据定义),您只能访问堆栈顶部或队列头部。这就是它们与
List
的区别。 (所以,这就是为什么你还没有找到一个)要回答,尽管你可以编写自己的,我会通过从
ObservableCollection
派生来完成,然后在实现的堆栈的情况下Push
作为Insert
在偏移量 0 处(并弹出作为返回索引 0,然后返回RemoveAt
索引 0);或者使用队列,您可以将Add
添加到列表末尾以Enqueue
,然后抓取并删除第一个项目,就像堆栈一样,用于Dequeue
。将在基础ObservableCollection
上调用Insert
、Add
和RemoveAt
操作,从而导致CollectionChanged
事件被触发。您也可能会说,您只是想在您应该有权访问的一项发生更改时进行绑定或收到通知。您可以再次创建自己的类(从 Stack 或 Queue 派生),并在以下情况下手动触发 CollectionChanged 事件:
With Stacks and Queues (almost by definition) you only have access to the top of the stack or head of the queue. It's what differentiates them from a
List
. (and so, that's why you haven't found one)To answer though you could write your own, I would do it by deriving from
ObservableCollection
, then in the case of a stack implementing thePush
as anInsert
at offset 0 (and pop as returning index 0 thenRemoveAt
index 0); or with a queue you could justAdd
to the end of the list toEnqueue
, and the grab and remove the first item, as with the stack, forDequeue
. TheInsert
,Add
andRemoveAt
operations would be called on the underlyingObservableCollection
and so cause theCollectionChanged
event to be fired.You might also be saying that you simply want to bind or be notified when the one item you are supposed to have access to changes. You would create your own class again, derived from Stack or Queue, and fire the CollectionChanged event manually when:
我意识到已经有一些答案,但我想我会回馈一些我的答案。我将帖子和评论中提到的所有内容放在一起。有几件事促使我这样做:
Push
、Pop
或ClearCount
code> 被调用,如其中一篇文章中提到的。Clear
,操作应为Reset
,并且集合更改事件的索引应设置为-1
(如果不是,它将默认为设置以便其他帖子也有该设置): .NET docsPush
/Pop
,操作应为Add
/Remove
并且集合的索引已更改对于堆栈来说,事件应该是0
,因为它始终是并且只是第一个可以操作的项目(想想stack.GetEnumerator().MoveNext()
)。Stack
中可用的所有 3 个构造函数并使用base()
调用,因为没有理由重写逻辑。结果:
I realize there are already a few answers but figured I would give back a little with mine. I put together everything mentioned in the posts and comments. There were few things that motivated me to do this:
Count
whenPush
,Pop
, orClear
are called, as mentioned in one of the posts.Clear
, action should beReset
and index for the collection change event should be set to-1
(which it will default to anyway if not set so the other posts have that): .NET docsPush
/Pop
, action should beAdd
/Remove
and index for the collection changed event should be0
for a stack being that it is always and only the first item that can be maniuplated (thinkstack.GetEnumerator().MoveNext()
).Stack<T>
and usebase()
calls since there is no reason to override the logic.Results in:
与上面的类非常相似,但有一些例外:
Very similar to the above class, with a few exceptions: