Код: Выделить всё
[Serializable]
[DebuggerDisplay($"Count = {{{nameof(Count)}}}, Size = {{{nameof(Size)}}}")]
public class FixedSizeQueue : IReadOnlyCollection, INotifyCollectionChanged, INotifyPropertyChanged
{
private readonly Queue _queue = new();
private readonly object _lock = new();
public event NotifyCollectionChangedEventHandler CollectionChanged;
public event PropertyChangedEventHandler PropertyChanged;
public int Count { get { lock (_lock) { return _queue.Count; } } }
public int Size { get; }
public FixedSizeQueue(int size) {
ArgumentOutOfRangeException.ThrowIfLessThan(size, 1);
Size = size;
}
public FixedSizeQueue(IEnumerable collection) {
if (collection is null || !collection.Any()) throw new ArgumentException("Cannot initialize the Queue with a null or empty collection", nameof(collection));
_queue = new Queue(collection);
Size = _queue.Count;
}
public void Enqueue(T obj) {
lock (_lock) {
if (_queue.Count == Size) _ = Dequeue();
_queue.Enqueue(obj);
OnCollectionChanged(NotifyCollectionChangedAction.Add, [obj], _queue.Count - 1);
}
}
public T Dequeue() {
var item = Dequeue(1);
OnCollectionChanged(NotifyCollectionChangedAction.Remove, [item], 0);
return item;
}
public T Dequeue(int count) {
var item = default(T);
while (Count > 0) {
item = _queue.Dequeue();
}
return item;
}
public void Clear() {
lock (_lock) {
_queue.Clear();
OnCollectionChanged(NotifyCollectionChangedAction.Reset, null);
}
}
public IEnumerator GetEnumerator() {
lock (_lock) return new List(_queue).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
protected virtual void OnCollectionChanged(NotifyCollectionChangedAction action, List items, int? index = null) {
if (index.HasValue)
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, items, index.Value));
else
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, items));
OnPropertyChanged(nameof(Count));
}
protected virtual void OnPropertyChanged(string propName)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
< /code>
Исключение брошено в строке 53 < /p>
CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(action, items, index.Value));
< /code>
as < /p>
System.InvalidOperationException
HResult=0x80131509
Message=Added item does not appear at given index '0'.
Source=PresentationFramework
StackTrace:
at MS.Internal.Data.EnumerableCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args)
at Project.FixedSizeQueue`1.OnCollectionChanged(NotifyCollectionChangedAction action, List`1 items, Nullable`1 index) in FixedSizeQueue.cs:line 53
at Project.FixedSizeQueue`1.Dequeue() in FixedSizeQueue.cs:line 30
at Project.FixedSizeQueue`1.Enqueue(T obj) in FixedSizeQueue.cs:line 23
at Project.MainWindow.c__DisplayClass24_0.b__1() in MainWindow.xaml.cs:line 607
at System.Windows.Threading.DispatcherOperation.InvokeDelegateCore()
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
< /code>
строка, которая не удается в Mainwindow, < /p>
Dispatcher.Invoke(() => Report.Enqueue(report));
Очередь связана с пользовательским элементом управления как < /p>
Код: Выделить всё
... custom styling and grid view setup
protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue) {
base.OnItemsSourceChanged(oldValue, newValue);
if (oldValue as INotifyCollectionChanged != null)
(oldValue as INotifyCollectionChanged).CollectionChanged -= ItemsCollectionChanged;
if (newValue as INotifyCollectionChanged == null) return;
(newValue as INotifyCollectionChanged).CollectionChanged += ItemsCollectionChanged;
}
< /code>
void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
if (_scrollViewer == null) return;
if (!_scrollViewer.VerticalOffset.Equals(_scrollViewer.ScrollableHeight)) return;
UpdateLayout();
_scrollViewer.ScrollToBottom();
}
< /code>
I'm looking for something to use to help track down the issue leading to a resolution. I'm sure there's additional detail I need to provide, but I'm not sure where to go next on that front.
Подробнее здесь: https://stackoverflow.com/questions/796 ... ound-queue
Мобильная версия