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

标签 c# xamarin mvvm xamarin.android 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我不能有一个构造函数,因为自动映射器需要默认的空构造函数......所以问题是我需要将删除命令绑定(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/

相关文章:

c# - 桌面应用 Realm

java - 在 Xamarin Forms Android 中打印到 POS 打印机

c# - 多个 UserControl 中的 WPF ToolBarControl

android - Xamarin.Forms Android 在 VS 2019 上启动时崩溃

c# - 可以在 Xamarin Forms 中使用的汉堡包/抽屉下拉菜单?

.net - 如何使用 DataContext 属性在 XAML 中的窗口上设置 ViewModel?

c# - 如何在项目MVVM Toolkit中的ViewModel之间共享信息?

c# - 如何将gridview添加到弹出窗口中?

c# - 如何在 C# 中获取当前的 DNS 服务器?

c# - C#线程dataGridView和加载