C# ListView:NotifyCollectionChangedEventHandler始终引发ArgumentOutOfRangeException
我有一个C# ListView:NotifyCollectionChangedEventHandler始终引发ArgumentOutOfRangeException,c#,uwp,C#,Uwp,我有一个ListView控件,我使用实现ISupportIncrementalLoading/INotifyCollectionChanged的集合类异步加载该控件(通过设置ListView.ItemsSource) 当我尝试调用INotifyCollectionChanged事件订阅服务器时,我得到ArgumentOutOfRangeException“此集合无法处理大于Int32.MaxValue-1(0x7FFFFFFFFF-1)的索引。\r\n参数名称:index”异常 我尝试了Noti
ListView
控件,我使用实现ISupportIncrementalLoading
/INotifyCollectionChanged
的集合类异步加载该控件(通过设置ListView.ItemsSource
)
当我尝试调用INotifyCollectionChanged
事件订阅服务器时,我得到ArgumentOutOfRangeException
“此集合无法处理大于Int32.MaxValue-1(0x7FFFFFFFFF-1)的索引。\r\n参数名称:index”异常
我尝试了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>();
}
}
公共类GroupDataSource:ObservableCollection,
INotifyCollectionChanged,
i支持递增加载,其中T:SDataEntity,new()
{
...
异步任务NotifyOfInsertedItems(int baseIndex,int count)
{
等待Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
foreach(事件处理程序中的NotifyCollectionChangedEventHandler处理程序)
{
for(int i=0;i
{
//跳过-异步加载数据
int baseIndex=_items.Count;
锁定(_项)
{
_items.AddRange(新结果);
}
等待插入项的通知(baseIndex、newResults.Count);
返回新的LoadMoreItemsResult(){Count=(uint)newResults.Count};
}
}).AsAsAsyncOperation();
}
}
值得一提的是,在我从
列表
而不是可观测集合
派生类之后,问题就解决了