更新与MVVM后台工作一个的ObservableCollection [英] Update a ObservableCollection with a background worker in mvvm

查看:1066
本文介绍了更新与MVVM后台工作一个的ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我最近实施了后台工作进行保存和加载数据。

Ok, I recently implemented a background worker to perform saves and loading of data.

但得到这个在save命令的工作已经证明困难的。

However getting this to work on a save command has proved difficult.

基本上我保存命令生成通知该产品已添加集合视图模型的事件,它应该将它添加到自己的observeablecollection。在这一点上,我得到了平时的异常说我可以更新在不同的线程一个ICollection的。我曾尝试创建一个调用Dispatcher.Invoke然而,这仍然会产生相同的异常新的列表类型,我想知道是否有人有其他人以及如何最好地解决这个建议吗?

Basically my Save command generates an event that notifies a collection view model that an Item has been added and that It should add it to its own observeablecollection. At this point I get the usual exception saying I can update a ICollection on a different thread. I have tried creating a new list type that calls Dispatcher.Invoke however this still generates the same exception, I was wondering whether anyone else had and suggestions on how best to tackle this?

所以,目前我有,从观察到的集合继承的类

So currently I have a class that Inherits from Observable collection

public class ThreadSafeObservableCollection : ObservableCollection
{

    public ThreadSafeObservableCollection(List<T> collection)
        : base(collection)
    {
        dispatcher = Dispatcher.CurrentDispatcher;
        rwLock = new ReaderWriterLock();
    }

    protected override void InsertItem(int index, T item)
    {

        if (dispatcher.CheckAccess())
        {
            if (index > this.Count)
                return;
            LockCookie c = rwLock.UpgradeToWriterLock(-1);
            base.InsertItem(index, item);
            rwLock.DowngradeFromWriterLock(ref c);
        }
        else
        {
            object[] o = new object[] { index, item };
            dispatcher.Invoke(DispatcherPriority.Send, (SendOrPostCallback)delegate { InsertItemImpl(o); }, o);
        }
    }

然后我有一个具有后台工作的开展视图模型类保存。

I Then Have a view model class that has a background worker that carries out the save.

一旦保存完成触发一个事件到另一个视图模型,以更新其列表。

Once the save is complete an event is fired to another view model to update its list.

    protected override void OnObjectAddedToRepository(object sender, ObjectEventArgs<cdAdministrators> e)
    {
        Dispatcher x = Dispatcher.CurrentDispatcher;
        AdministratorViewModel viewModel = new AdministratorViewModel(e.EventObject, DataAccess);
        viewModel.RecentlyAdded = true;
        viewModel.ItemSelected += this.OnItemSelected;
        this.AllViewModels.Add(viewModel);
        RecentlyAddedViewModel = viewModel;

        OnPropertyChanged(null);
    }

这两个名单是由一个单独的后台辅助线程创建的。

Both lists are created by a seperate background worker thread.

推荐答案

您已经得到了code这更增加了观察的集合的项目(在视图模型$ P ​​$ psumably),换行添加调用在 Dispatcher.BeginInvoke 电话。

Where you've got code which adds the item to the observable collection (presumably in the view model), wrap that Add call in a Dispatcher.BeginInvoke call.

诚然,这意味着视图模型需要知道调度,然后成为尴尬的测试......好在它不是太难引入自己的 IDispatcher 接口和使用依赖注入以正常的方式。

Admittedly that means the view model needs to know about the dispatcher, which then becomes awkward to test... fortunately it's not too hard to introduce your own IDispatcher interface and use dependency injection in the normal way.

这篇关于更新与MVVM后台工作一个的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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