.NET:ArgumentOutOfRangeException在收集调用。新增的时候(与故障Pivot控件) [英] .NET: ArgumentOutOfRangeException when calling .Add on a collection (Trouble with Pivot control)

查看:1059
本文介绍了.NET:ArgumentOutOfRangeException在收集调用。新增的时候(与故障Pivot控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一个例外,但我不明白为什么:

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}

堆栈跟踪:

   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 是一个ObservableCollection,当前包含 T

dest is an ObservableCollection, that currently contains t.

是一个列表,其中包含7名成员。

source is a List, that contains 7 members.

T

为什么会这样发生?

更新 DEST 的ItemsSource 透视。这透视正在监听 dest.CollectionChanged ,我相信这是造成问题的原因。

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

在code以上的关键部分是重新索引源的一部分。如果我评论说出来,我不明白这个问题,也没有许多其他错误(例如对于 PivotItem 是空的内容,或者错误的 PivotItem 被选中。)

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:

<一个href="http://stackoverflow.com/questions/5056029/argument-invalid-exception-while-navigating/5076501#5076501">Argument无效异常而导航

这篇关于.NET:ArgumentOutOfRangeException在收集调用。新增的时候(与故障Pivot控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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