【问题标题】:ArgumentOutOfRangeException in MvxTableViewSource.GetItemAtMvxTableViewSource.GetItemAt 中的 ArgumentOutOfRangeException
【发布时间】:2016-04-13 08:45:15
【问题描述】:

我有一个继承自 MvxSimpleTableViewSource 的自定义 TableViewSource:

public class MyTableViewSource : MvxSimpleTableViewSource
{
    private readonly MyViewModel viewModel;

    public MyTableViewSource(MyViewModel viewModel, UITableView tableView)
        : base(tableView, MyTeaserCell.Key, MyTeaserCell.Key)
    {
        this.viewModel = viewModel;
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return this.viewModel.Items.Count; 
    }
}

有时,我没有覆盖的 GetItemAt 会引发 ArgumentOutOfRangeException。

堆栈跟踪是:

Parameter name: index', reason: 'System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
 at System.ThrowHelper.ThrowArgumentOutOfRangeException () <0x2bef74 +     0x00038> in <filename unknown>:0 
at System.Collections.Generic.List`1[T].get_Item (Int32 index) <0x19f620 + 0x0002f> in <filename unknown>:0 
at System.Collections.ObjectModel.Collection`1[T].System.Collections.IList.get_Item (Int32 index) <0x1a6ff4 + 0x0004f> in <filename unknown>:0 
at MvvmCross.Binding.ExtensionMethods.MvxEnumerableExtensions.ElementAt (IEnumerable items, Int32 position) <0x8fceac + 0x00103> in <filename unknown>:0 
at MvvmCross.Binding.iOS.Views.MvxTableViewSource.GetItemAt (Foundation.NSIndexPath indexPath) <0x958830 + 0x00053> in <filename unknown>:0 
at MvvmCross.Binding.iOS.Views.MvxBaseTableViewSource.GetCell (UIKit.UITableView tableView, Foundation.NSIndexPath indexPath) <0x957940 + 0x0002f> in <filename unknown>:0 
at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) <0x834b50 + 0x00033> in <filename unknown>:0 
at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x834a78 + 0x000cb> in <filename unknown>:0 
at App.Application.Main (System.String[] args) <0xc66cc + 0x0002f> in <filename unknown>:0 '

目前我无法清楚地重现此异常,但每天会发生 2 到 3 次。 在我看来,有时在 ItemsSource 有项目之前调用 GetItemAt。

【问题讨论】:

    标签: ios xamarin mvvmcross


    【解决方案1】:

    您不需要自定义类MyTableViewSource,直接使用MvxSimpleTableViewSource即可

    var source = new MvxSimpleTableViewSource (tableView, MyTeaserCell.Key, MyTeaserCell.Key);
    
    //apply binding
    set.Bind(source).To(vm=>vm.Items);
    

    另一种方式在MyTableViewSource里面,只需要删除RowsInSection。它已经用ItemsSource.Count实现了

    【讨论】:

    • 我使用了一个自定义类,因为我知道我需要实现一些其他的功能,比如编辑等等。但是删除RowsInSection 的建议听起来不错。我会试一试...
    猜你喜欢
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多