ListView: NotifyCollectionChangedEventHandler always raises ArgumentOutOfRangeException

0

I have a ListView control which I load asynchronously (by setting ListView.ItemsSource) using a collection class that implements ISupportIncrementalLoading / INotifyCollectionChanged.

When I attempt to invoke the INotifyCollectionChanged event subscriber, I get ArgumentOutOfRangeException "This collection cannot work with indices larger than Int32.MaxValue - 1 (0x7FFFFFFF - 1).\r\nParameter name: index" exception.

I tried just about every overload of the NotifyCollectionChangedEventArgs constructor. No matter what I do, I always get the same exception. I do not see anything wrong with my NotifyCollectionChangedEventArgs object that I pass to the event handler.

Any ideas?

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>();
                    }
        }

enter image description here

c#
uwp

1 Answer

1

For what it is worth, the problem went away after I derived the class from List<T> rather than ObservableCollection<T>.


User contributions licensed under CC BY-SA 3.0