异步增加的ObservableCollection(或替代) [英] Asynchronously adding to ObservableCollection (or an alternative)

查看:350
本文介绍了异步增加的ObservableCollection(或替代)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我有 - 有一个的ItemsSource设置为一个ListBox一个的ObservableCollection< T> - 其中T是我的自定义类重presenting的文件,只包含2 DependencyProperties:文件名和ThumbnailPath。 - 列表框还定义了一个自定义的DataTemplate,以便在它很好地显示图像和文件名

Here's what I have - a ListBox with an ItemsSource set to a ObservableCollection<T> - where T is my custom class representing a file, containing just 2 DependencyProperties: Filename and ThumbnailPath. - The listbox also has a custom DataTemplate defined, in order to nicely display a image and filename under it.

列表框的目的是要显示的视频文件在当前文件夹(在树视图中选择),以缩略图(异步生成,这不是问题的一部分)

The purpose of the listbox is to display video files in the current folder (selected in a TreeView), with thumbnails (generated asynchronously; not part of this problem).

所以,当我更改文件夹在树视图中,的ObservableCollection被清除,填补了一遍,它会自动反映在ListBox中的项目。

So when I change the folder in the TreeView, the ObservableCollection is cleared and filled up again, which is automatically reflected in the the ListBox items.

下面的问题:用户界面变得没有反应,它更新最多需要几秒钟。此外,缩略图没有意义在这里(我尝试禁用它们)。 我觉得什么花费时间最多的是我的自定义类的实例50-100的建设,他们的视觉重新presentation - 它初始化为每一个Image对象。但是,这只是我的猜测 - ?能否请你确认或排除这种可能性

Here's the problem: The UI becomes unresponsive and it takes up to several seconds to update. Again, thumbnails do not have significance here (I tried disabling them). I think what takes the most time is the construction of 50-100 instances of my custom class, and their visual representation - it has to initialize an Image object for each one. But it's just my guess - could you please confirm or exclude the possibility?

我开始觉得的ObservableCollection未必要走的路在这里,因为从我读,从我试用了一下,有没有办法,至少如果这些异步添加项目,项目DependencyObjects。我试着用一个BackgroundWorker创建我的类实例并将它们添加到在ProgressChanged事件处理程序的集合,但它抛出一个异常(某些线程VS dependencyobjects问题)。

I'm beginning to think ObservableCollection may not the way to go here, since from what I read and a little from what I tried, there's no way to add items asynchronously, at least if these items are DependencyObjects. I tried creating my class instances with a BackgroundWorker and adding them to the collection in the ProgressChanged event handler, but it throws an exception (some threading vs dependencyobjects problem).

有什么我失踪?我会不会变得更好,只需开沟的ObservableCollection,写一个良好的老异步循环添加的项目?

Is there something that I'm missing? Or would I be better off by simply ditching the ObservableCollection and writing a good old async for loop to add the items?

推荐答案

由于您的的ObservableCollection 势必UI因此它被UI线程产生,所以任何进一步的更新(删除/添加/清除)必须是相同的UI线程。它不允许从其他线程更新

Since your ObservableCollection is bound to UI hence it gets generated on UI thread so any further updates (delete/add/clear) have to be on the same UI thread. It doesn't allow updates from another thread.

不过,你所能做的就是创建类的insance(或全部费时的后台线程操作),一旦你完成,使用的ObservableCollection添加对象调度这样你的UI线程 -

However, what you can do is create insance of your class (or all time consuming operation on background thread) and once you are done, add the object in ObservableCollection using Dispatcher of your UI thread like this -

App.Current.Dispatcher.BeginInvoke((Action)delegate()
                          {
                              observableCollection.Add(instanceOfYourClass);
                          });

什么调度做的就是把操作其相关的线程。因此,该项目将始终在UI线程增加,但可以在后台线程创建。

What Dispatcher do is put the operation on its associated thread. Hence, the item will always be added on UI thread but can be created in background thread.

下面是几个环节可能让你去 - 从BW 等一个<一个href="http://stackoverflow.com/questions/3628477/update-a-observablecollection-with-a-background-worker-in-mvvm"标题=点击这里>这里

Here are few links which might get you going - Updating from BW and other one here

这篇关于异步增加的ObservableCollection(或替代)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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