Observable Collection 上的集合更改事件示例
我在 WPF 应用程序中有一个列表框,它显示可观察的照片对象集合。当照片添加到集合中时,UI 需要立即显示新图像。我知道这可以使用 CollectionChanged 事件来处理。我已经查找了有关如何使用处理集合更改事件的示例,但我还没有找到任何有效的方法。有谁知道有什么好的例子吗?
另一件事是,图像来自我计算机上的目录,并且我有一个文件系统观察程序监视该目录中是否添加或删除了新照片。我目前正在使用文件系统事件处理程序在添加或删除照片时更新集合,但问题是当我向目录添加新照片时,会抛出异常,表示我无法从不是该线程的线程修改集合主线程。有谁也知道如何解决这个问题?这是解决这个问题的代码:
public class PhotoList : ObservableCollection<Photo>
{
DirectoryInfo _directory;
private FileSystemWatcher _watcher;
public PhotoList()
{
_watcher = new FileSystemWatcher();
MessageBox.Show("Watching..");
_watcher.Filter = "*.jpg";
_watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
_watcher.EnableRaisingEvents = true;
_watcher.Created += new FileSystemEventHandler(FileSystemWatcher_Created);
_directory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
}
public void Update()
{
foreach(FileInfo f in _directory.GetFiles("*.jpg"))
{
Add(new Photo(f.FullName));
}
}
public string Path
{
set
{
_directory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
Update();
}
get
{
return _directory.FullName;
}
}
public void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
Add(new Photo(e.FullPath));
}
}
I have listbox in a WPF application that displays an observable collection of photo objects. When a photo is added to the collection the UI needs show the new image right away. I understand this can be handled using the CollectionChanged event. I have looked around for examples on how to use the handle collection changed events but I havent found anything that worked. Does anyone know of any good examples?
Another thing is that the images are coming from a directory on my computer and i have a file system watcher watching the directory for new photos being added or deleted. I am currently using file system event handler to update the collections when a photo is added or deleted but the problem is when i add a new photo to the directory, an exception is thrown saying that I cant modify the collection from a thread thats not the main thread. Does anyone know how to solve this problem too? Here is the code for this problem:
public class PhotoList : ObservableCollection<Photo>
{
DirectoryInfo _directory;
private FileSystemWatcher _watcher;
public PhotoList()
{
_watcher = new FileSystemWatcher();
MessageBox.Show("Watching..");
_watcher.Filter = "*.jpg";
_watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
_watcher.EnableRaisingEvents = true;
_watcher.Created += new FileSystemEventHandler(FileSystemWatcher_Created);
_directory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
}
public void Update()
{
foreach(FileInfo f in _directory.GetFiles("*.jpg"))
{
Add(new Photo(f.FullName));
}
}
public string Path
{
set
{
_directory = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
Update();
}
get
{
return _directory.FullName;
}
}
public void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
Add(new Photo(e.FullPath));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一个问题:
ObservableCollection
已经实现了INotifyCollectionChanged
,因此只需绑定该对象即可,UI 将自动获取集合中发生的更新。第二个问题:请参阅这篇文章:WPF 线程:我可以在非 UI 线程中更新控件的数据上下文吗? 以及随附的注释。
First question:
ObservableCollection<T>
already implementsINotifyCollectionChanged
, so simply bind on that object and you are fine, the UI will get the updates happening in the collection automatically.Second question: See this post: WPF threading: can I update a control's data context in a non-UI thread? and the accompening comments.
将 Add(new Photo(e.FullPath)) 包装在 Dispatcher.Invoke() 内。这样 Add 将在 UI 线程上被调用
wrap the Add(new Photo(e.FullPath)) inside of Dispatcher.Invoke(). That way Add will be called on the UI thread