首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ObservableCollection不引发CollectionChnaged

ObservableCollection不引发CollectionChnaged
EN

Stack Overflow用户
提问于 2017-04-25 11:58:56
回答 1查看 56关注 0票数 0

我查了几个ObservableCollection的实现,下面是我最后得到的结果。在启动服务时,我得到了添加触发的事件,但是当我调用并更新列表中的对象的属性时,集合根本不通知或触发。

ObservableCollection处理程序

代码语言:javascript
复制
public class iGamingObservableCollection 
{
    ObservableCollection<GameInstance> _contentList;
    public iGamingObservableCollection()
    {
        _contentList = new ObservableCollection<GameInstance>();
        _contentList.CollectionChanged += ContentCollectionChanged;
    }
    public ObservableCollection<GameInstance> ContentList
    {
        get { return _contentList; }
    }

    public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            foreach (GameInstance item in e.OldItems)
            {
                Console.WriteLine("removed: State is now: " + item.InstanceState);
                //Removed items

            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (GameInstance item in e.NewItems)
            {
                Console.WriteLine("added: State is now: " + item.InstanceState);
                //Added items
                //item.PropertyChanged += InstancePropertyChange;
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Replace)
        {
            foreach (GameInstance item in e.NewItems)
            {
                Console.WriteLine("changed: State is now: " + item.InstanceState);
                //Changed items

                //item.PropertyChanged += InstancePropertyChange;

            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Replace)
        {
            foreach (GameInstance item in e.OldItems)
            {
                Console.WriteLine("changed: State is now: " + item.InstanceState);
                //Changed items

                //item.PropertyChanged += InstancePropertyChange;

            }
        }
    }

ObservableCollection控制器

代码语言:javascript
复制
public class InstanceManagerService : IInstanceManager
{
    //TODO: FIX THAT CLASS.. THIS IS INITIAL IMPLEMENTATION!!!
    IGameInstanceRepository instanceRepository;
    iGamingObservableCollection notifyList = new iGamingObservableCollection();
    //private Timer updateNotifierTimer;
    public InstanceManagerService(IGameInstanceRepository instanceRepository)
    {
        this.instanceRepository = instanceRepository;
        populateNotifyList();
        //updateNotifierTimer = new Timer((e) => { populateNotifyList(); }, null, 120 , (int)(TimeSpan.FromMinutes(2).TotalMilliseconds));
    }

    private void populateNotifyList()
    {
        var list = instanceRepository.getInstances();

        foreach (var l in list)
        {
            notifyList.ContentList.Clear();
            notifyList.ContentList.Add(l);
        }
        Console.WriteLine("Notify list content updated.");
    }

public bool releaseInstance(Guid instanceId)
    {
        if (instanceRepository.UpdateGameInstanceToFree(instanceId))
        {
            var notifyTask = notifyList.ContentList.Where(g => g.Id == instanceId).FirstOrDefault();
            notifyTask.InstanceState = InstanceState.Free;

            return true;
        }
        return false;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-25 12:03:55

但是,当我调用和更新列表中的对象的属性时,集合根本不通知或触发。

ObservableCollection不是这样工作的。当在集合中添加/删除对象时,就会触发CollectionChanged。这就是为什么NotifyCollectionChangedAction没有Updated值的原因。要做您想做的事情,在类中实现INotififyPropertyChanged

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43610058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档