MvxTableViewSource.GetItemAt

时间:2016-04-13 08:45:16

标签: ios xamarin mvvmcross

我有一个继承自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。

1 个答案:

答案 0 :(得分:0)

您不需要自定义课程MyTableViewSource,只需直接使用MvxSimpleTableViewSource

var source = new MvxSimpleTableViewSource (tableView, MyTeaserCell.Key, MyTeaserCell.Key);

//apply binding
set.Bind(source).To(vm=>vm.Items);
在MyTableViewSource中

另一种方式,只需要删除 RowsInSection。已经使用ItemsSource.Count

实现了它