.NET:在集合上调用.Add时的ArgumentOutOfRangeException(与数据透视控件有关的问题) [英] .NET: ArgumentOutOfRangeException when calling .Add on a collection (Trouble with Pivot control)

查看:152
本文介绍了.NET:在集合上调用.Add时的ArgumentOutOfRangeException(与数据透视控件有关的问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个例外,但我不明白为什么:

  public static void AddAll< T>这个ICollection< T> dest,ICollection< T>来源)
{
if(dest == null)
{
throw new ArgumentNullException(dest);
}

foreach(来源的T t)
{
//参数超出范围异常
//消息:\r\ nParameter name:index
dest.Add(t);
}
}

异常是:

  $ exception {\r\\\
Parameter name:index} System.Exception {System.ArgumentOutOfRangeException}

Stacktrace:

 在System.ThrowHelper。 ThrowArgumentOutOfRangeException(ExceptionArgument参数,ExceptionResource资源)
在System.ThrowHelper.ThrowArgumentOutOfRangeException()
在System.Collections.Generic.List`1.get_Item(Int32索引)
在System.Collections.Generic System.Windows.Controls.ItemCollection.GetItemImpl(Int32索引)
在System.Windows.Controls.ItemCollection.GetItemImplSkipMethodPack(Int32)中的.List`1.System.Collections.IList.get_Item(Int32索引)
索引)
在System.Windows.PresentationFrameworkCollection`1.get_Item(Int32索引)
在Microsoft.Phone.Controls.Primitives.PivotHeadersControl.FadeInItemIfNeeded(Int32索引,Int32 visualFirstIndex,Int32以前的VisualisInfo,Int32 itemCount)
在Microsoft.Phone.Controls.Primitives.PivotHeadersControl.UpdateItemsLayout()
在Microsoft.Phone.Controls.Primitives.PivotHeadersControl.OnItemsChanged(NotifyCollectionChangedEventArgs e)
在系统。 Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)
在System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
在System.Windows.Controls.ItemCollection.UpdateItemsSourceList(IEnumerable newItemsSource)
在System.Windows.Controls.ItemsControl.ItemsSourceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
在System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp,Object newValue,Object oldValue)
在系统。 Windows.DependencyObject.SetValueInternal(DependencyProperty dp,Object value,Boolean allowReadOnlySet,Boolean isSetByStyle,Boolean isSetByB在System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp,Object value)中的
$ System.Windows.DependencyObject.SetValue(DependencyProperty dp,Object value)

在System.Windows.DependencyObject.SetValueInternal Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
在Microsoft.Phone.Controls.Pivot.UpdateHeaders()
在Microsoft.Phone.Controls.Pivot.OnItemsChanged(NotifyCollectionChangedEventArgs e)
在System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)
在System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
在System.Windows.Controls.ItemCollection.System.Windows .Controls.ICollectionChangedListener.OnCollectionChanged(Object sender,NotifyCollectionChangedEventArgs e)
在System.Windows.Controls.WeakCollectionChangedListener.SourceCollectionChanged(Object sender,NotifyCollectionChanged EventArgs e)System.Collections.ObjectModel.ObservableCollection中的
()SystemCollectionChanged(NotifyCollectionChangedEventArgs e)
在System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32索引,SectionViewModel项)
在System.Collections.ObjectModel.Collection`1.Add(SectionViewModel项)
在MyApp.ExtensionMethods.AddAll [T](ICollection`1 dest,ICollection`1源)
在MyApp.Sections.get_SectionViewModels ()
在System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi,Object obj,BindingFlags invokeAttr,Binder binder,Object参数,CultureInfo culture,Boolean isBinderDefault,Assembly caller,Boolean verifyAccess,StackCrawlMark& stackMark)
在System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,BindingFlags invokeAttr,Binder binder,Object [] parameters,CultureInfo culture,StackCrawlMark& stackMark)
在System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo System.Windows.CLRPropertyListener.get_Value()
在System.Reflection.RuntimePropertyInfo.GetValue(Object obj,Object在System.Windows.PropertyAccessPathStep.get_Value()中的
System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep源)
$ System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener source)

System.Windows.CLRPropertyListener.SourcePropertyChanged(Object sender,PropertyChangedEventArgs args)
在System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(Object sender,PropertyChangedEventArgs args )
在MyApp.Sections.onPropChanged(String name)
在MyApp.Sections.Sections_Loaded(Object sender,RoutedEventArgs e)
在System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex,Delegate handlerDelegate ,Object sender,Object args)
在MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj,IntPtr unmanagedObjArgs,Int32 argsTypeIndex,String eventName)

dest 是一个ObservableCollection,当前包含 t



是一个包含7个成员的列表。



t source 的第一个成员。



为什么可以这样吗?



更新 dest Pivot 的I​​temsSource 。 Pivot 正在收听 dest.CollectionChanged ,我相信这是导致该问题的。

 < controls:Pivot Title =SECTIONSx:Name =pivotControlItemsSource ={Binding SectionViewModels}> <! -  ...  - > < controls:Pivot /> 

以下是枢纽控件绑定到的属性:

  private ObservableCollection< SectionViewModel> _sectionViewModels; 
public ObservableCollection< SectionViewModel> SectionViewModels
{
get
{
//如果我们还没有加载
if(NavigationContext == null)
{
返回null;
}

如果(_sectionViewModels == null)
{
_sectionViewModels = new ObservableCollection< SectionViewModel>();
_sectionViewModels.AddAll(SunData.GetSections()。选择(section => new SectionViewModel(section))ToList());

foreach(_ViewViewModel中的SectionViewModel sectionViewModel)
{
SunData.GetStories(sectionViewModel.Section,()=>
{
onPropChanged(LoadingBarVisibility );
});
}
}

//重新索引源代码,所以我们不会得到一个烦人的转换动画
// http://stackoverflow.com/questions / 4541020 /
int activeVID = int.Parse(NavigationContext.QueryString [Section.SectionsKey]);

SectionViewModel selectedItem = _sectionViewModels.Where(sectionVM => sectionVM.Section.Vid == activeVID).Single();
int selectedIndex = _sectionViewModels.IndexOf(selectedItem);

//一种方法reindex
for(int i = 0; i< selectedIndex; i ++)
{
SectionViewModel sectionVM = _sectionViewModels [0];
_sectionViewModels.Remove(sectionVM);
_sectionViewModels.Add(sectionVM);
}

//另一种reindex方式
/ *
IList< SectionViewModel> reindexedSectionVMs = new List< SectionViewModel>();
(int i = 0; reindexedSectionVMs.Count!= _sectionViewModels.Count; i ++)
{
reindexedSectionVMs.Add(_sectionViewModels [(selectedIndex + i)%_sectionViewModels.Count]);
}

_sectionViewModels.Clear();
_sectionViewModels.AddAll(reindexedSectionVMs);
* /
return _sectionViewModels;
}
}

上面代码中的关键部分是索引源部分。如果我注意到这一点,我没有得到这个问题,也没有其他的错误(比如一个 PivotItem 的内容是空的,或错误的 PivotItem 被选中。)



我已经尝试了两种重新索引列表的方法,但都会导致相同的错误。 / p>

我不知道我在做什么是导致这些问题。

解决方案

我在我的问题中找到了解决方案:



导航时参数无效异常


I'm getting an exception, but I don't understand why:

    public static void AddAll<T>(this ICollection<T> dest, ICollection<T> source)
    {
        if (dest == null)
        {
            throw new ArgumentNullException("dest");
        }

        foreach (T t in source)
        {
            // Argument out of range exception
            // Message: "\r\nParameter name: index"
            dest.Add(t);
        }
    }

The exception is:

$exception  {"\r\nParameter name: index"}   System.Exception {System.ArgumentOutOfRangeException}

Stacktrace:

   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at System.ThrowHelper.ThrowArgumentOutOfRangeException()
   at System.Collections.Generic.List`1.get_Item(Int32 index)
   at System.Collections.Generic.List`1.System.Collections.IList.get_Item(Int32 index)
   at System.Windows.Controls.ItemCollection.GetItemImpl(Int32 index)
   at System.Windows.Controls.ItemCollection.GetItemImplSkipMethodPack(Int32 index)
   at System.Windows.PresentationFrameworkCollection`1.get_Item(Int32 index)
   at Microsoft.Phone.Controls.Primitives.PivotHeadersControl.FadeInItemIfNeeded(Int32 index, Int32 visualFirstIndex, Int32 previousVisualFirstIndex, Int32 itemCount)
   at Microsoft.Phone.Controls.Primitives.PivotHeadersControl.UpdateItemsLayout()
   at Microsoft.Phone.Controls.Primitives.PivotHeadersControl.OnItemsChanged(NotifyCollectionChangedEventArgs e)
   at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Windows.Controls.ItemCollection.UpdateItemsSourceList(IEnumerable newItemsSource)
   at System.Windows.Controls.ItemsControl.ItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object newValue, Object oldValue)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet, Boolean isSetByStyle, Boolean isSetByBuiltInStyle, PropertyInvalidationReason reason)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at System.Windows.Controls.ItemsControl.set_ItemsSource(IEnumerable value)
   at Microsoft.Phone.Controls.Pivot.UpdateHeaders()
   at Microsoft.Phone.Controls.Pivot.OnItemsChanged(NotifyCollectionChangedEventArgs e)
   at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Windows.Controls.ItemCollection.NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Windows.Controls.ItemCollection.System.Windows.Controls.ICollectionChangedListener.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Windows.Controls.WeakCollectionChangedListener.SourceCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.InsertItem(Int32 index, SectionViewModel item)
   at System.Collections.ObjectModel.Collection`1.Add(SectionViewModel item)
   at MyApp.ExtensionMethods.AddAll[T](ICollection`1 dest, ICollection`1 source)
   at MyApp.Sections.get_SectionViewModels()
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.InternalGetValue(PropertyInfo thisProperty, Object obj, Object[] index, StackCrawlMark& stackMark)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at System.Windows.CLRPropertyListener.get_Value()
   at System.Windows.PropertyAccessPathStep.get_Value()
   at System.Windows.PropertyPathListener.RaisePropertyPathStepChanged(PropertyPathStep source)
   at System.Windows.PropertyAccessPathStep.RaisePropertyPathStepChanged(PropertyListener source)
   at System.Windows.CLRPropertyListener.SourcePropertyChanged(Object sender, PropertyChangedEventArgs args)
   at System.Windows.Data.WeakPropertyChangedListener.PropertyChangedCallback(Object sender, PropertyChangedEventArgs args)
   at MyApp.Sections.onPropChanged(String name)
   at MyApp.Sections.Sections_Loaded(Object sender, RoutedEventArgs e)
   at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

dest is an ObservableCollection, that currently contains t.

source is a List, that contains 7 members.

t is the first member of source.

Why could this be happening?

Update: dest is the ItemsSource for a Pivot. That Pivot is listening for dest.CollectionChanged, which I believe is causing the problem.

<controls:Pivot Title="SECTIONS" x:Name="pivotControl" ItemsSource="{Binding SectionViewModels}"> <!-- ... --> <controls:Pivot />

Here is the property that the pivot control is binding to:

        private ObservableCollection<SectionViewModel> _sectionViewModels;
        public ObservableCollection<SectionViewModel> SectionViewModels
        {
            get
            {
                // don't do anything if we haven't loaded yet
                if (NavigationContext == null)
                {
                    return null;
                }

                if (_sectionViewModels == null)
                {
                    _sectionViewModels = new ObservableCollection<SectionViewModel>();
                    _sectionViewModels.AddAll(SunData.GetSections().Select(section => new SectionViewModel(section)).ToList());

                    foreach (SectionViewModel sectionViewModel in _sectionViewModels)
                    {
                        SunData.GetStories(sectionViewModel.Section, () =>
                            {
                                onPropChanged("LoadingBarVisibility");
                            });
                    }
                }

                // re-index the source so we don't get an annoying transition animation
                // http://stackoverflow.com/questions/4541020/
                int activeVID = int.Parse(NavigationContext.QueryString[Section.SectionsKey]);

                SectionViewModel selectedItem = _sectionViewModels.Where(sectionVM => sectionVM.Section.Vid == activeVID).Single();
                int selectedIndex = _sectionViewModels.IndexOf(selectedItem);

                // one way to reindex
                for (int i = 0; i < selectedIndex; i++)
                {
                    SectionViewModel sectionVM = _sectionViewModels[0];
                    _sectionViewModels.Remove(sectionVM);
                    _sectionViewModels.Add(sectionVM);
                }

                // another way to reindex
/*
                IList<SectionViewModel> reindexedSectionVMs = new List<SectionViewModel>();
                for (int i = 0; reindexedSectionVMs.Count != _sectionViewModels.Count; i++)
                {
                    reindexedSectionVMs.Add(_sectionViewModels[(selectedIndex + i) % _sectionViewModels.Count]);
                }

                _sectionViewModels.Clear();
                _sectionViewModels.AddAll(reindexedSectionVMs);
*/
                return _sectionViewModels;
            }
        }

The key part in the code above is the re-indexing the source part. If I comment that out, I don't get this issue, nor a host of other bugs (like the content for a PivotItem being empty, or the wrong PivotItem being selected.)

I've tried two methods of reindexing the list, but they both lead to the same bugs.

I'm not sure what I'm doing here that's causing these issues.

解决方案

i found the solution to this in my problem:

Argument invalid exception while navigating

这篇关于.NET:在集合上调用.Add时的ArgumentOutOfRangeException(与数据透视控件有关的问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆