我不知道我做错了什么,我按照教程(https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/master/N-16-CollectABull-Part5/CollectABull.Core/ViewModels/ListViewModel .cs)关于如何绑定列表并在选择特定列表项时拉出详细信息屏幕。我的问题是当一个项目被选中时,什么也没有发生。我正在使用流利的绑定来处理数据绑定,因为我希望利用抽象活动/视图模型。部分代码片段如下:
我的列表布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/project"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxListView android:id="@+id/List"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
local:MvxItemTemplate="@layout/list_item"
/>
</RelativeLayout>
我的活动:
public class List : AbstractList {
public new ListView ViewModel{
get { return (ListView) base.ViewModel; }
set { base.ViewModel = value; }
}
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
var set = this.CreateBindingSet<List, ListView>();
set.Bind(list).For(x => x.ItemsSource).To(vm => vm.List);
set.Bind(list).For(x => x.ItemClick).To(vm => vm.ShowDetail);
set.Apply();
}
}
我的抽象活动:
public class AbstractList : MvxActivity{
protected Legacy.LegacyBar actionBar;
protected MvxListView list;
protected override void OnCreate(Bundle bundle) {
base.OnCreate(bundle);
RequestWindowFeature(WindowFeatures.NoTitle);
SetContentView(Resource.Layout.list);
actionBar = FindViewById<Legacy.LegacyBar>(Resource.Id.actionbar);
list = FindViewById<MvxListView>(Resource.Id.List);
actionBar.SetGravity(GravityFlags.Center);
actionBar.Title = Title;
}
}
我的视图模型:
public class ListView : AbstractListView<Item>{
private readonly IRepository repository;
public ListView(IRepository repo){
repository= repo;
DataBind();
}
public List<Item> List{
get { return list; }
set{
list = value;
RaisePropertyChanged(() => List);
}
}
protected override Task<QueryResult<Item>> DataBindCall(){
return repository.Get(new Query());
}
protected override void SetResultList(Item[] resultList){
List = resultList.ToList();
}
}
我的抽象视图模型:
public abstract class AbstractListView<T> : MvxViewModel where T: class, new (){
protected List<T> list;
public IMvxCommand ShowDetail {
get {
return new MvxCommand<Item>(item => ShowViewModel<ItemView>(new ItemView.Navigation() { Id = item.ID }));
}
}
protected abstract Task<QueryResult<T>> DataBindCall();
protected abstract void SetResultList(T[] resultList);
protected void DataBind(){
DataBindCall().ContinueWith(x => {
SetResultList(x.Result.Results);
});
}
}
当我单步执行代码时,我可以看到它在绑定调用时在 ShowDetail 上中断,但是,在加载列表并单击列表中的一项后,它什么也不做。在我看来绑定不成功,但没有错误消息或来自 logcat 的任何内容。我也尝试过手动拉出我的详细信息页面,并且加载正确,因此详细信息页面本身没有问题。我是否将绑定机制与我引入的所有抽象类混淆了?还是有一些我看不到的非常明显的错误。提前感谢您的帮助!