如何在MvxListView中绑定MvxListView中的ItemClick [英] How to bind ItemClick in MvxListView in MvxListView

查看:19
本文介绍了如何在MvxListView中绑定MvxListView中的ItemClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我想将 ItemClick Command 绑定到 MvxListView 中的项目.这里我的 ViewModel 中有一个 Person 列表,其中包含一个 Dog 列表.

In my example below I want to bind an ItemClick Command to the Item in the MvxListView. Here I have in my ViewModel a List of Person that contains a List of Dog.

ItemsSource HasDogs 绑定工作正常.

当 MvvmCross 尝试将 ItemClick SelectDogCommand 绑定到 Viewmodel 中的 ICommand 时,我收到此异常.

When MvvmCross is trying to bind ItemClick SelectDogCommand to the ICommand in the Viewmodel I get this Exception.

[0:] 
MvxBind:Warning: 11,30 Unable to bind: source property source not found Property:SelectDogCommand on Person
[0:] MvxBind:Warning: 11,30 Unable to bind: source property source not found Property:SelectDogCommand on Person
12-04 15:05:03.062 I/mono-stdout(16338): MvxBind:Warning: 11,30 Unable to bind: source property source not found Property:SelectDogCommand on Person

希望能帮到你.

这是我的例子:

public class FirstViewModel:MvxViewModel
{
    private List<Person> _persons;
    public List<Person> Persons
    {
      get { return _persons; }
      set { _persons = value; }
    }

    private Cirrious.MvvmCross.ViewModels.MvxCommand<Dog> _selectDog;
    public System.Windows.Input.ICommand SelectDogCommand
    {
        get
        {
            _selectDog = _selectDog ?? new Cirrious.MvvmCross.ViewModels.MvxCommand<Dog>(SelectDog);
            return _selectDog;
        }
    }

    private void SelectDog(Dog item)
    {
        ShowViewModel<DetailViewModel>(new DetailViewModel.Parameters{dog = item});
    }

}

public class Person
{
    private string _name;
    private List<Dog> _hasDogs;

    public List<Dog> HasDogs
    {
      get { return _hasDogs; }
      set { _hasDogs = value; }
    }

    public string Name
    {
      get { return _name; }
      set { _name = value; }
    }
}

public class Dog{...}

Android 视图 Xml:

The Android View Xml:

第一视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    ...>
    <TextView ...
        local:MvxBind="Text Persons"
    <Mvx.MvxListView
        ...
        local:MvxBind="ItemsSource Persons"
        local:MvxItemTemplate="@layout/item_person" />
</LinearLayout>

item_person:

item_person:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    ...
    android:layout_height="200dp">
    <TextView
        ...
        local:MvxBind="Text Name" />
    <Mvx.MvxListView
        ...
        local:MvxBind="ItemsSource HasDogs; ItemClick SelectDogCommand"
        local:MvxItemTemplate="@layout/item_dog" />
</LinearLayout>

推荐答案

你的人员列表项的 DataContext 是一个 Person - 所以你的 SelectDogCommand 需要是 Person 类的一部分 - 例如类似:

The DataContext for your person list item is a Person - so your SelectDogCommand needs to be part of the Person class - e.g. something like:

public class Person
{
    private string _name;
    private List<Dog> _hasDogs;

    public List<Dog> HasDogs
    {
      get { return _hasDogs; }
      set { _hasDogs = value; }
    }

    public string Name
    {
      get { return _name; }
      set { _name = value; }
    }

    private Cirrious.MvvmCross.ViewModels.MvxCommand<Dog> _selectDog;
    public System.Windows.Input.ICommand SelectDogCommand
    {
        get
        {
            _selectDog = _selectDog ?? new Cirrious.MvvmCross.ViewModels.MvxCommand<Dog>(dog => _parent.SelectDog(dog));
            return _selectDog;
        }
    }

    private FirstViewModel _parent;
    public Person(FirstViewModel parent)
    {
        _parent = parent;
    }
}

或者你可以让 Person 从 MvxNavigatingObject(或 MvxPropertyChanged 或 MvxViewModel)继承——在这种情况下,ShowViewModel 方法也将在那里可用.

or alternatively you could get Person to inherit from MvxNavigatingObject (or MvxPropertyChanged or MvxViewModel) - in which case the ShowViewModel methods will be available there too.

这篇关于如何在MvxListView中绑定MvxListView中的ItemClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆