如何在 C# 中创建 Observable Hashset?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/526986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:48:12  来源:igfitidea点击:

How can I make an Observable Hashset in C#?

c#wpfdata-binding

提问by Michael

Currently I am using an ObservableCollection within a WPF application, the application is an implementation of Conway's Game of life and works well for about 500 cells but after that it begins to slow down significantly. I originally wrote the application using a HashSet but couldn't find any way to bind the cells to a canvas.

目前,我在 WPF 应用程序中使用 ObservableCollection,该应用程序是康威生命游戏的实现,适用于大约 500 个单元格,但之后它开始显着减慢。我最初使用 HashSet 编写应用程序,但找不到任何方法将单元格绑定到画布。

Is there a way to get my HashSet to notify its binding object of changes? My Cell class is a simple integer X,Y pair, if the pair exists the cell is alive otherwise dead. The Cell implements INotifyPropertyChanged and overrides GetHashCode and Equals. I couldn't get the cell to display any changes, just the cells present immediately after loading. Is there any way to Bind a Hashset to items on a Canvas?

有没有办法让我的 HashSet 通知其绑定对象发生变化?我的 Cell 类是一个简单的整数 X,Y 对,如果该对存在,则细胞是活的,否则就死了。Cell 实现 INotifyPropertyChanged 并覆盖 GetHashCode 和 Equals。我无法让单元格显示任何更改,只有加载后立即出现的单元格。有没有办法将 Hashset 绑定到 Canvas 上的项目?

采纳答案by MrTelly

You have to implement INotifyCollectionChangedtoo, and then it should all work OK. There's another relevant SO answer which uses freezablesto ensure that changes in underlying entities are also handled.

你也必须实现INotifyCollectionChanged,然后它应该一切正常。还有另一个相关的 SO 答案,它使用 freezables来确保底层实体的更改也得到处理。

回答by Matt Hamilton

I don't know if this will help, but here's a reallysimple implementation of an "observable set" that I made for a personal project. It essentially guards against inserting (or overwriting with) an item that is already in the collection.

我不知道这是否会有所帮助,但这是我为个人项目制作的“可观察集”的一个非常简单的实现。它本质上防止插入(或覆盖)已经在集合中的项目。

If you wanted to you could simply return out of the methods rather than throwing an exception.

如果您愿意,您可以简单地从方法中返回而不是抛出异常。

public class SetCollection<T> : ObservableCollection<T> 
{
    protected override void InsertItem(int index, T item)
    {
        if (Contains(item)) throw new ItemExistsException(item);

        base.InsertItem(index, item);
    }

    protected override void SetItem(int index, T item)
    {
        int i = IndexOf(item);
        if (i >= 0 && i != index) throw new ItemExistsException(item);

        base.SetItem(index, item);
    }
}

回答by Geoff Cox

I've posted a complete ObservableHashSet here that you can use.

我在这里发布了一个完整的 ObservableHashSet 供您使用。

https://github.com/BellaCode/Public/tree/master/ObservableHashSet

https://github.com/BellaCode/Public/tree/master/ObservableHashSet

It is based of reflecting on how ObservableCollection is implemented and provides the same thread-safety re-entrancy checks.

它基于对 ObservableCollection 如何实现的反思,并提供相同的线程安全重入检查。