首页 > 解决方案 > ListView:NotifyCollectionChangedEventHandler 总是引发 ArgumentOutOfRangeException

问题描述

我有一个控件,我使用实现/的集合类ListView异步加载(通过设置) 。ListView.ItemsSourceISupportIncrementalLoadingINotifyCollectionChanged

当我尝试调用INotifyCollectionChanged事件订阅者时,我得到ArgumentOutOfRangeException此集合无法使用大于 Int32.MaxValue - 1 (0x7FFFFFFF - 1) 的索引。\r\n参数名称:索引”异常。

我几乎尝试了 NotifyCollectionChangedEventArgs 构造函数的所有重载。不管我做什么,我总是得到同样的例外。我没有看到传递给事件处理程序的 NotifyCollectionChangedEventArgs 对象有任何问题。

有任何想法吗?

public class GroupDataSource<T> : ObservableCollection<T>,
     INotifyCollectionChanged,                  
     ISupportIncrementalLoading  where T : SDataEntity, new()
{
        ...
        async Task NotifyOfInsertedItems(int baseIndex, int count)
        {
                        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                            () =>
                            {
                                foreach (NotifyCollectionChangedEventHandler handler in _eventHandlers)
                                {
                                    for (int i = 0; i < count; i++)
                                    {
                                        var args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, _items[i + baseIndex], i + baseIndex);
                                        try
                                        {
                                            handler(this, args);
                                        }
                                        catch(Exception e)
                                        {
                                            //todo: log it
                                            //this is where I get System.ArgumentOutOfRangeException
                                        }
                                    }
                                }
                            }
                            );
                    }

        public IAsyncOperation<LoadMoreItemsResult>LoadMoreItemsAsync(uint count)
        {

                        return Task.Run<LoadMoreItemsResult>(
                            async () =>
                            {
                                    //skipped - asynchronously load data

                                    int baseIndex = _items.Count;
                                    lock (_items)
                                    {
                                        _items.AddRange(newResults);
                                    }
                                    await NotifyOfInsertedItems(baseIndex, newResults.Count);

                                    return new LoadMoreItemsResult() { Count = (uint)newResults.Count };
                                }

                            }).AsAsyncOperation<LoadMoreItemsResult>();
                    }
        }

在此处输入图像描述

标签: c#uwp

解决方案


对于它的价值,在我从而List<T>不是ObservableCollection<T>.


推荐阅读