Csharp/C#教程:绑定到队列。 UI永远不会更新分享


绑定到队列。 UI永远不会更新

我将ListBox绑定到Queue 。 当我对项目进行入队/出列时, ListBox不会更新。

我有帮助入队/出队以提高财产变化

 protected void EnqueueWork(string param) { Queue.Enqueue(param); RaisePropertyChanged("Queue"); } protected string DequeueWork() { string tmp = Queue.Dequeue(); RaisePropertyChanged("Queue"); return tmp; } 

你有没有实现INotifyCollectionChanged ? 您需要此function来通知添加或删除集合中的项目等操作。

这是队列的简单实现:

 public class ObservableQueue : INotifyCollectionChanged, IEnumerable { public event NotifyCollectionChangedEventHandler CollectionChanged; private readonly Queue queue = new Queue(); public void Enqueue(T item) { queue.Enqueue(item); if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Add, item)); } public T Dequeue() { var item = queue.Dequeue(); if (CollectionChanged != null) CollectionChanged(this, new NotifyCollectionChangedEventArgs( NotifyCollectionChangedAction.Remove, item)); return item; } public IEnumerator GetEnumerator() { return queue.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } } 

您应该使用ObservableCollection而不是队列来执行您想要的操作,以允许ListBox更新项目添加和删除您的类应该实现INotifyCollectionChanged,ObservableCollection实现该接口,或者您可以编写实现INotifyCollectionChanged接口的自定义队列(ObservableQueue)

这篇文章可以帮助你

上述就是C#学习教程:绑定到队列。 UI永远不会更新分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1010796.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐