j'ai un MvxRecyclerView qui a le fichier AXML suivant:Comment câbler les actions de MvxRecyclerView article à ViewModel
<?xml version="1.0" encoding="utf-8"?>
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:MvxItemTemplate="@layout/item_detail"
app:MvxBind="ItemsSource Items" />
Correspodning ViewModel est défini comme ceci:
public class ItemsViewModel : MvxViewModel
{
private ObservableCollection<Models.Item> _items;
public ObservableCollection<Models.Item> Items
{
get { return _items; }
set
{
_items = value;
RaisePropertyChanged(() => Items);
}
}
public MvxCommand CommandToBeInvokedFromItem
{
get
{
return new MvxCommand(async() =>
{
await ...;
});
}
}
...
}
Mon item_detail AXML est défini comme ceci:
<?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_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
local:MvxBind="Text Name" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_delete_forever_black_24dp"
local:MvxBind="Click CommandToBeInvokedFromItem"/>
</LinearLayout>
Et Model.Item est défini comme ceci:
public class Item
{
public string Name { get; set; }
}
Première TextView se lie à la propriété de nom d'élément qui fonctionne très bien. Mais je veux que ImageButton se lie à une commande sur le ViewModel à laquelle MvxRecylerView est lié plutôt qu'à une propriété de l'Item. L'élément est juste un modèle et non un ViewModel. Comment puis-je accomplir cela?
Luke, merci mais c'est le deuxième comportement que je désire. Je veux câbler une commande à un clic sur le ImageButton qui se trouve dans la "cellule" MvxRecyclerView. Et oui, je me demandais si je pouvais le faire sans avoir d'élément à hériter de MvxViewModel. – Igor
Cela devrait fonctionner correctement sans hériter de 'MvxViewModel'. Il suffit de déplacer 'CommandToBeInvokedFromItem' à' Item'. –