使用 MvvmCross 从 Xamarin Android 中的 MvxListView 中删除项目
Delete item from MvxListView in Xamarin Android using MvvmCross
所以我有一个幻灯片列表:
SlideListView.axml:
<Mvx.MvxListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:elevation="0dp"
android:padding="5dp"
local:MvxItemTemplate="@layout/slidelistitem"
local:MvxBind="ItemsSource Slides" />
SlideListItemView.axml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/block"
android:elevation="2dp"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Test text" />
<Button
style="@style/ButtonSlide"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Delete" />
</LinearLayout>
所以每张幻灯片都有一个文本和删除按钮。
SlideListItemViewModel.cs:
public class SlideListItemViewModel : MvxViewModel
{
private long _id;
private string _title;
public long Id { get => _id; set => SetProperty(ref _id, value); }
public string Title { get => _title; set => SetProperty(ref _title, value); }
}
在SlideListItemViewModel
中我没有构造函数因为automapper需要默认的空构造函数...所以问题是我需要将删除命令绑定到SlideListViewModel
。我不知道我该怎么做...我还需要做些什么才能让所有删除逻辑都在 SlideListViewModel
而不是 SlideListItemViewModel
?
更新 1
那是我创建 SlideListItemViewModel
的地方
public class SlideListViewModel : MvxViewModel
{
private readonly IMvxNavigationService _navigation;
private ICollection<SlideListItemViewModel> _slides;
public ICollection<SlideListItemViewModel> Slides { get => _slides; set => SetProperty(ref _slides, value); }
public SlideListViewModel(IMvxNavigationService navigation)
{
_navigation = navigation;
}
}
您可以在 SlideListItemViewModel
中定义一个 MvxCommand
(例如称为 DeleteCommand)。它应该在触发时使用 MvxMessenger Plugin 发布一条消息,指示需要删除该项目。
您需要使用以下方法将此 MvxCommand 绑定到您的按钮:
local:MvxBind:"Click DeleteCommand"
最后,您还需要订阅 SlideListViewModel
中的消息,并在收到新消息时从列表中删除该项目。
我会告诉你我是如何在我的项目中使用的,你会适应你的:
1) 创建一个 class 你 "wrap" 实体和一个命令:
public class EntityWrap<T>
{
private Action<T> _realPrimaryCommand { get; set; }
public T Entity { get; set; }
public ICommand PrimaryCommand { get; set; }
public EntityWrap(T entity, Action<T> realPrimaryCommand)
{
Entity = entity;
_realPrimaryCommand = realPrimaryCommand;
PrimaryCommand = new MvxCommand(() => _realPrimaryCommand(entity));
}
}
2) 我的书class:
public class Book
{
public int Id { get; set; }
public string Name { get; set }
public Book(int id, string name)
{
Id = id;
Name = name;
}
}
3) 在视图模型中:
public BooksViewModel : MvxViewModel
{
public BooksViewModel()
{
var books = new List<Book>() { new Book(1, "AAA"), new Book(2, "BBB"), new Book(3, "CCC") };
Books = new ObservableCollection<EntityWrap<Book>>(books.Select(x => new EntityWrap<Book>(x, async y => await DoDeleteBookCommand(y))));
}
private ObservableCollection<EntityWrap<Book>> _books;
public ObservableCollection<EntityWrap<Book>> Books
{
get { return _books; }
set
{
_books = value;
RaisePropertyChanged(() => Books);
}
}
private async Task DoDeleteBookCommand(Book book)
{
var bookToRemove = Books.FirstOrDefault(x => x.Entity.Id == book.Id);
if (bookToRemove != null)
{
//Your code...
Books.Remove(bookToRemove);
}
}
}
4) 在你的项目布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
local:MvxBind="Text Entity.Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Delete"
local:MvxBind="Click PrimaryCommand" />
</LinearLayout>
不要在 ListView/RecyclerView 上设置 ItemClick,除非您有用于行点击的命令。
所以我有一个幻灯片列表:
SlideListView.axml:
<Mvx.MvxListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:elevation="0dp"
android:padding="5dp"
local:MvxItemTemplate="@layout/slidelistitem"
local:MvxBind="ItemsSource Slides" />
SlideListItemView.axml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/block"
android:elevation="2dp"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Test text" />
<Button
style="@style/ButtonSlide"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Delete" />
</LinearLayout>
所以每张幻灯片都有一个文本和删除按钮。
SlideListItemViewModel.cs:
public class SlideListItemViewModel : MvxViewModel
{
private long _id;
private string _title;
public long Id { get => _id; set => SetProperty(ref _id, value); }
public string Title { get => _title; set => SetProperty(ref _title, value); }
}
在SlideListItemViewModel
中我没有构造函数因为automapper需要默认的空构造函数...所以问题是我需要将删除命令绑定到SlideListViewModel
。我不知道我该怎么做...我还需要做些什么才能让所有删除逻辑都在 SlideListViewModel
而不是 SlideListItemViewModel
?
更新 1
那是我创建 SlideListItemViewModel
public class SlideListViewModel : MvxViewModel
{
private readonly IMvxNavigationService _navigation;
private ICollection<SlideListItemViewModel> _slides;
public ICollection<SlideListItemViewModel> Slides { get => _slides; set => SetProperty(ref _slides, value); }
public SlideListViewModel(IMvxNavigationService navigation)
{
_navigation = navigation;
}
}
您可以在 SlideListItemViewModel
中定义一个 MvxCommand
(例如称为 DeleteCommand)。它应该在触发时使用 MvxMessenger Plugin 发布一条消息,指示需要删除该项目。
您需要使用以下方法将此 MvxCommand 绑定到您的按钮:
local:MvxBind:"Click DeleteCommand"
最后,您还需要订阅 SlideListViewModel
中的消息,并在收到新消息时从列表中删除该项目。
我会告诉你我是如何在我的项目中使用的,你会适应你的:
1) 创建一个 class 你 "wrap" 实体和一个命令:
public class EntityWrap<T>
{
private Action<T> _realPrimaryCommand { get; set; }
public T Entity { get; set; }
public ICommand PrimaryCommand { get; set; }
public EntityWrap(T entity, Action<T> realPrimaryCommand)
{
Entity = entity;
_realPrimaryCommand = realPrimaryCommand;
PrimaryCommand = new MvxCommand(() => _realPrimaryCommand(entity));
}
}
2) 我的书class:
public class Book
{
public int Id { get; set; }
public string Name { get; set }
public Book(int id, string name)
{
Id = id;
Name = name;
}
}
3) 在视图模型中:
public BooksViewModel : MvxViewModel
{
public BooksViewModel()
{
var books = new List<Book>() { new Book(1, "AAA"), new Book(2, "BBB"), new Book(3, "CCC") };
Books = new ObservableCollection<EntityWrap<Book>>(books.Select(x => new EntityWrap<Book>(x, async y => await DoDeleteBookCommand(y))));
}
private ObservableCollection<EntityWrap<Book>> _books;
public ObservableCollection<EntityWrap<Book>> Books
{
get { return _books; }
set
{
_books = value;
RaisePropertyChanged(() => Books);
}
}
private async Task DoDeleteBookCommand(Book book)
{
var bookToRemove = Books.FirstOrDefault(x => x.Entity.Id == book.Id);
if (bookToRemove != null)
{
//Your code...
Books.Remove(bookToRemove);
}
}
}
4) 在你的项目布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="match_parent"
local:MvxBind="Text Entity.Name" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Delete"
local:MvxBind="Click PrimaryCommand" />
</LinearLayout>
不要在 ListView/RecyclerView 上设置 ItemClick,除非您有用于行点击的命令。