gpt4 book ai didi

xamarin.android - 如何在 MvxListView 中绑定(bind) MvxListView 中的 ItemClick

转载 作者:行者123 更新时间:2023-12-04 06:54:30 25 4
gpt4 key购买 nike

在下面的示例中,我想将 ItemClick 命令绑定(bind)到 MvxListView 中的项目。
在这里,我的 ViewModel 中有一个包含狗列表的人员列表。

ItemsSource HasDogs 绑定(bind)工作正常。

当 MvvmCross 试图将 ItemClick SelectDogCommand 绑定(bind)到 Viewmodel 中的 ICommand 时,我得到了这个异常。

[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 View XML:

第一 View :
<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:
<?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 的一部分类 - 例如就像是:

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 方法也将在那里可用。

关于xamarin.android - 如何在 MvxListView 中绑定(bind) MvxListView 中的 ItemClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20377851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com