gpt4 book ai didi

c# - 可观察的堆栈和队列

转载 作者:可可西里 更新时间:2023-11-01 03:13:01 27 4
gpt4 key购买 nike

我正在寻找 StackQueueINotifyCollectionChanged 实现。我可以自己动手,但我不想重新发明轮子。

最佳答案

我遇到了同样的问题,想与其他人分享我的解决方案。希望这对某人有帮助。

public class ObservableStack<T> : Stack<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
public ObservableStack()
{
}

public ObservableStack(IEnumerable<T> collection)
{
foreach (var item in collection)
base.Push(item);
}

public ObservableStack(List<T> list)
{
foreach (var item in list)
base.Push(item);
}


public new virtual void Clear()
{
base.Clear();
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}

public new virtual T Pop()
{
var item = base.Pop();
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
return item;
}

public new virtual void Push(T item)
{
base.Push(item);
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}


public virtual event NotifyCollectionChangedEventHandler CollectionChanged;


protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
this.RaiseCollectionChanged(e);
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
this.RaisePropertyChanged(e);
}


protected virtual event PropertyChangedEventHandler PropertyChanged;


private void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (this.CollectionChanged != null)
this.CollectionChanged(this, e);
}

private void RaisePropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, e);
}


event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add { this.PropertyChanged += value; }
remove { this.PropertyChanged -= value; }
}
}

关于c# - 可观察的堆栈和队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3127136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com