MvxExpandableListView,每个组/项都有一个viewmodel

时间:2017-11-09 11:51:40

标签: c# android mvvm xamarin.android mvvmcross

我正在尝试为每个组/项创建一个简单的MvxExpandableListView,其中包含一个viewmodel。解决方案构建,并且列表首先看起来很好(当它没有展开时)但是当我尝试扩展组时我得到一个例外:

11-08 17:47:39.333 I/MonoDroid(19993): System.InvalidCastException: Specified cast is not valid.
11-08 17:47:39.333 I/MonoDroid(19993):   at MvvmCross.Binding.Droid.Views.MvxExpandableListAdapter.GetChildrenCount (System.Int32 groupPosition) [0x00000] in C:\projects\mvvmcross\MvvmCross\Binding\Droid\Views\MvxExpandableListAdapter.cs:122 
11-08 17:47:39.333 I/MonoDroid(19993):   at Android.Widget.IExpandableListAdapterInvoker.n_GetChildrenCount_I (System.IntPtr jnienv, System.IntPtr native__this, System.Int32 groupPosition) [0x00008] in <d278c06ad5684d6882c743a94a93ebc2>:0 
11-08 17:47:39.333 I/MonoDroid(19993):   at (wrapper dynamic-method) System.Object:374f56da-1ca6-4999-88d3-2d5ade6299d7 (intptr,intptr,int)

这是布局:

<Mvx.MvxExpandableListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/TripsExpandableListView"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="@null"
android:childDivider="@android:color/transparent"
local:MvxBind="ItemsSource Trips"
local:MvxItemTemplate="@layout/mytrips_listitem"
local:MvxGroupItemTemplate="@layout/mytrips_listheader"/>

这是来自viewmodel的代码,其中包含MvxExpandableListView:

private MvxObservableCollection<TripViewModel> trips;
public MvxObservableCollection<TripViewModel> Trips
{
     get { return trips; }
     set
     {
         trips = value;
         RaisePropertyChanged(() => Trips);
     }
}
public override void Prepare(Parameter parameter)
{
    Trips = new MvxObservableCollection<TripViewModel>();
    for (int i = 0; i < SharedResources.Trips.Count; i++)
    {
        var trip = SharedResources.Trips[i];
        Trips.Add(new TripViewModel(trip.Distance.ToString(), trip.StopsNumber.ToString()));
    }
}

我想要做的是为每个组(TripViewModel)创建一个viewmodel,为每个项目设置一个viewmodel(StopViewModel)。我怎样才能做到这一点?提前谢谢!

0 个答案:

没有答案