ObservableCollection不会引发CollectionChnaged

时间:2017-04-25 11:58:57

标签: c# events event-handling observablecollection

我查了几个ObservableCollection的实现,这就是我最终的结果。我在启动服务时启动了ADD启动事件,但是当我执行调用并更新列表中对象的属性时,集合根本不会通知或触发。

ObservableCollection处理程序。

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控制器

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;
    }

1 个答案:

答案 0 :(得分:3)

  

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

ObservableCollection无法正常工作。在其集合中添加/删除对象时,它会CollectionChanged触发。这就是NotifyCollectionChangedAction没有Updated价值的原因。要做你想做的事,在课堂上实施INotififyPropertyChanged