如何在C#中创建可观察的Hashset? [英] How can I make an Observable Hashset in C#?

查看:140
本文介绍了如何在C#中创建可观察的Hashset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在WPF应用程序中使用了一个ObservableCollection,该应用程序是Conway的生命之游戏的实现,并且在500个单元格中运行良好,之后它开始显着减慢。我最初使用HashSet编写了应用程序,但找不到任何方式将单元格绑定到画布。



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

解决方案

您必须实现 INotifyCollectionChanged ,然后它应该都可以正常工作。还有一个相关的SO答案,使用freezables 以确保基础实体的更改也处理。


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.

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?

解决方案

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

这篇关于如何在C#中创建可观察的Hashset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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