ObservableQueue?

前端 未结 2 883
借酒劲吻你
借酒劲吻你 2021-01-28 07:25

Has anyone written a version of .Net\'s generic Queue that implements INotifyCollectionChanged, or is there one hidden deep in the .Net framework somewhere already?

2条回答
  •  孤独总比滥情好
    2021-01-28 07:48

    A quick search didn't show any results. But the interface is simple and it would be almost trivial to extend the Queue class and add support for the interface. Just override all methods thusly:

    // this isn't the best code ever; refactor as desired
    protected void OnCollectionChanged( NotifyCollectionChangedEventArgs ccea){
      var temp = CollectionChanged;
      if(temp != null) temp(this, ccea);  
    }
    
    // and later in the class
    
    public override SomeMethodThatAltersTheQueue(object something){
      // record state of collection prior to change
      base.SomeMethodThatAltersTheQueue(something)
      // create NotifyCollectionChangedEventArgs with prior state and new state
      OnCollectionChanged(ccea);
    }
    

提交回复
热议问题