gpt4 book ai didi

c# - 使用 MvvmCross 从 Xamarin Android 中的 MvxListView 中删除项目

转载 作者:行者123 更新时间:2023-12-03 10:57:15 25 4
gpt4 key购买 nike

所以我有一张幻灯片列表:

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我不能有一个构造函数,因为自动映射器需要默认的空构造函数......所以问题是我需要将删除命令绑定(bind)到 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;
}
}

最佳答案

我将向您展示我如何在我的项目中使用并适应您的:

1)创建一个“包装”你的实体和命令的类:

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)我的书课:
public class Book
{
public int Id { get; set; }
public string Name { get; set }

public Book(int id, string name)
{
Id = id;
Name = name;
}
}

3)在 View 模型中:
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,除非您有行单击命令。

关于c# - 使用 MvvmCross 从 Xamarin Android 中的 MvxListView 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46705074/

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