【问题标题】:Delete item from MvxListView in Xamarin Android using MvvmCross使用 MvvmCross 从 Xamarin Android 中的 MvxListView 中删除项目
【发布时间】:2017-10-12 08:40:20
【问题描述】:

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

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 我不能有一个构造函数,因为自动映射器需要默认的空构造函数......所以问题是我需要将删除命令绑定到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?你不能从那里将父级传递给模型吗?
  • @Cheesebaron 更新了我的问题。并且没有不能将父级传递给模型
  • @NikasŽalias 你能看看我的回答是否能解决你的问题吗?

标签: c# xamarin mvvm xamarin.android mvvmcross


【解决方案1】:

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

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) 在视图模型中:

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。

【讨论】:

    【解决方案2】:

    您可以在您的SlideListItemViewModel 中定义一个MvxCommand(例如称为DeleteCommand)。它应该在触发时使用MvxMessenger Plugin 发布消息,指示需要删除该项目。

    您需要使用以下方法将此 MvxCommand 绑定到您的按钮:

    local:MvxBind:"Click DeleteCommand"

    最后,您还需要订阅SlideListViewModel 中的消息,并在收到新消息时从列表中删除该项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2022-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多