c# - ListView:NotifyCollectionChangedEventHandler 总是引发 ArgumentOutOfRangeException
问题描述
我有一个控件,我使用实现/的集合类ListView
异步加载(通过设置) 。ListView.ItemsSource
ISupportIncrementalLoading
INotifyCollectionChanged
当我尝试调用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>();
}
}
解决方案
对于它的价值,在我从而List<T>
不是ObservableCollection<T>
.
推荐阅读
- python - Python中的混合字符串
- reactjs - React Native 保存旧的文本输入数据
- visual-studio-code - SFML - VScode - CMAKE 项目 - 项目运行良好,但标题上出现曲线
- mysql - MySQL - 不和谐 - 发布表/结果
- javascript - 如何将脚本移动到 div 容器中
- apache-kafka - 如何在不重新部署的情况下让 Flink 作业生成可变数量的 Kafka 主题?
- reactjs - 使用 Azure AD 通过 React.js(前端)和 Flask(后端)对用户进行身份验证
- mysql - Django:groupby 获得第一行
- c - 当我按 Enter 时,Scanf 没有跳过
- python - 尝试根据索引的值将列表过滤到另一个列表中