使用Android的Mvvmcross,ItemClick不会启动第二个ViewModel

时间:2013-11-06 18:39:13

标签: android mvvmcross

我无法弄清楚我在这方面做错了什么,我按照教程(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的任何内容。我也尝试手动提取我的详细信息页面,并且正确加载,因此详细信息页面本身没有问题。我是否将绑定机制与我介绍的所有抽象类混淆了?还是有一些我看不到的非常明显的错误。在此先感谢您的帮助!

0 个答案:

没有答案