MvvmCross 6 RecyclerView 多个按钮项目绑定
MvvmCross 6 RecyclerView multiple buttons item binding
我在 Xamarin 和 MvvmCross 方面是个新手。目前,我成功地做了一些基本的事情。
但是现在,我面临一个简单的问题(对我来说)。我得到了 MvxRecyclerView
。它的每个项目都有 2 个按钮。我该如何绑定它们?
鉴于您的 ViewModels:
public class MyViewModel : MvxViewModel
{
public MyViewModel()
{
this.MyItems.Add(new MyItemViewModel());
this.MyItems.Add(new MyItemViewModel());
}
public ObservableCollection<MyItemViewModel> MyItems { get; set; } = new ObservableCollection<MyItemViewModel>();
}
public class MyItemViewModel : MvxNotifyPropertyChanged
{
public MyItemViewModel()
{
// Initialize your commands
}
public ICommand MyCommand1 { get; set; }
public ICommand MyCommand2 { get; set; }
}
在您看来:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<mvvmcross.droid.support.v7.recyclerview.MvxRecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
local:MvxItemTemplate="@layout/item_test"
local:MvxBind="ItemsSource MyItems" />
</LinearLayout>
在您的项目视图中 item_test.axml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My button 1"
local:MvxBind="Click MyCommand1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My button 2"
local:MvxBind="Click MyCommand2" />
</LinearLayout>
高
我在 Xamarin 和 MvvmCross 方面是个新手。目前,我成功地做了一些基本的事情。
但是现在,我面临一个简单的问题(对我来说)。我得到了 MvxRecyclerView
。它的每个项目都有 2 个按钮。我该如何绑定它们?
鉴于您的 ViewModels:
public class MyViewModel : MvxViewModel
{
public MyViewModel()
{
this.MyItems.Add(new MyItemViewModel());
this.MyItems.Add(new MyItemViewModel());
}
public ObservableCollection<MyItemViewModel> MyItems { get; set; } = new ObservableCollection<MyItemViewModel>();
}
public class MyItemViewModel : MvxNotifyPropertyChanged
{
public MyItemViewModel()
{
// Initialize your commands
}
public ICommand MyCommand1 { get; set; }
public ICommand MyCommand2 { get; set; }
}
在您看来:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<mvvmcross.droid.support.v7.recyclerview.MvxRecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
local:MvxItemTemplate="@layout/item_test"
local:MvxBind="ItemsSource MyItems" />
</LinearLayout>
在您的项目视图中 item_test.axml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My button 1"
local:MvxBind="Click MyCommand1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My button 2"
local:MvxBind="Click MyCommand2" />
</LinearLayout>
高