C# &引用;指定的参数超出了有效值的范围。参数名称:index";将新项目添加到ObservableCollection时<;T>;
我有这样一个例外: 'FooStorageStorage.Add(新的TreeViewItem(){Header=i.ToString()})' 引发了类型为“System.ArgumentOutOfRangeException”的异常: “指定的参数超出了有效值的范围。\r\n参数 名称:索引“ 我在viewModel中有一个属性:C# &引用;指定的参数超出了有效值的范围。参数名称:index";将新项目添加到ObservableCollection时<;T>;,c#,wpf,mvvm,viewmodel,observablecollection,C#,Wpf,Mvvm,Viewmodel,Observablecollection,我有这样一个例外: 'FooStorageStorage.Add(新的TreeViewItem(){Header=i.ToString()})' 引发了类型为“System.ArgumentOutOfRangeException”的异常: “指定的参数超出了有效值的范围。\r\n参数 名称:索引“ 我在viewModel中有一个属性: private ObservableCollection<TreeViewItem> fooStorage=new ObservableCollect
private ObservableCollection<TreeViewItem> fooStorage=new ObservableCollection<TreeViewItem>();
public ObservableCollection<TreeViewItem> FooStorage
{
get { return facetStorage; }
set { facetStorage = value; }
}
我有一个以上的例外
当我第一次调用LoadData()方法时,一切正常。然后,如果我在第二次调用LoadData()方法,就会得到这样一个异常
有人遇到过这样的例外吗?最有趣的是,我不能在测试项目中重现这个异常
堆栈跟踪:
System.ArgumentOutOfRangeException未经处理,HResult=-2146233086
Message=指定的参数超出有效值的范围。
参数名称:索引参数名称=索引源=PresentationCore堆栈跟踪: 位于System.Windows.Media.VisualCollection.Insert(Int32索引,可视) 位于System.Windows.Controls.UIElementCollection.InsertInternal(Int32 索引,UIElement(元素) 在System.Windows.Controls.Panel.AddChildren(GeneratorPosition pos,Int32 itemCount)中 位于System.Windows.Controls.Panel.OnItemChangedInternal(对象发送器,ItemsChangedEventArgs参数) 在System.Windows.Controls.Panel.OnItemChanged(对象发送器,ItemsChangedEventArgs args) 位于System.Windows.Controls.ItemContainerGenerator.OnItemMadded(对象 项目,Int32索引) 在System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(对象 发送方,NotifyCollectionChangedEventArgs(参数) 在System.Windows.EventManager.ListenerList
1.DeliverEvent(对象
发件人,事件参数,类型managerType)
位于System.Windows.EventManager.DeliverEvent(对象发送者,事件参数)
位于System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(对象
发送方,NotifyCollectionChangedEventArgs(参数)
在System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(对象
发件人,NotifyCollectionChangedEventArgs(e)
位于System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs
args)
在System.Windows.Controls.ItemCollection.OnViewCollectionChanged(对象
发件人,NotifyCollectionChangedEventArgs(e)
在System.Windows.DeliventManager.ListenerList中
1.DeliverEvent(对象
发件人,事件参数,类型managerType)
位于System.Windows.EventManager.DeliverEvent(对象发送者,事件参数)
位于System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(对象
发送方,NotifyCollectionChangedEventArgs(参数)
位于System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs
args)
位于System.Windows.Data.ListCollectionView.ProcessCollectionChangedWithAdjustedIndex(NotifyCollectionChangedEventArgs
参数,Int32调整后的索引,Int32调整后的新索引)
位于System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs
args)
在System.Windows.Data.CollectionView.OnCollectionChanged(对象发送方,
NotifyCollectionChangedEventArgs(参数)
位于System.Collections.ObjectModel.ObservableCollection1.OnCollectionChanged(NotifyCollectionChangedEventArgs
(e)
位于System.Collections.ObjectModel.ObservableCollection
1.InsertItem(Int32
索引(T项)
在System.Collections.ObjectModel.Collection1.Add(T项)
在D:\WPF\…\ViewModel\PersonControlViewModel.cs中的ModuleA.ViewModel.PersonControlViewModel.LoadData(IPerson person)处:第110行
在Prism.Commands.DelegateCommand中
1.c_uuu显示类别1_0.b_u0(对象
o)
在Prism.Commands.DelegateCommandBase.c__DisplayClass5_0.b__0(对象
arg)
在Prism.Commands.DelegateCommandBase.d_u14.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务
(任务)
在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
(任务)
在Prism.Commands.DelegateCommandBase.d_u12.MoveNext()中
---来自引发异常的上一个位置的堆栈结束跟踪---
在System.Runtime.CompilerServices.AsyncMethodBuilderCore.c.b_uu6_0(对象
(州)
位于System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托
回调,对象args,Int32 numArgs)
在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源、,
委托回调、对象参数、Int32 numArgs、委托catchHandler)
在System.Windows.Threading.DispatcherOperation.InvokeImpl()中
在System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象
(州)
位于System.Threading.ExecutionContext.RunInternal(ExecutionContext
executionContext、ContextCallback回调、对象状态、布尔值
(同步CTX)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔值
(同步CTX)
在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)
在MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext
executionContext、ContextCallback回调、对象状态)
在System.Windows.Threading.DispatcherOperation.Invoke()中
在System.Windows.Threading.Dispatcher.ProcessQueue()中
位于System.Windows.Threading.Dispatcher.WndProcHook(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled)
在MS.Win32.HwndWrapper.WndProc(IntPtr-hwnd、Int32-msg、IntPtr-wParam、IntPtr-lParam、Boolean和handled)
在Wi女士
private void LoadData()
{
if (FooStorage.Count > 0)
{
FooStorage.Clear();
}
for (int k = 0; k < lengthOfColl; k++)
{
FooStorage.Add(new TreeViewItem() { Header=k.ToString()});//here is exception
}
}