在Xamarin

时间:2019-05-06 19:16:21

标签: c# listview xamarin observablecollection imagesource

我将放置代码片段,最后放入我遇到的问题的结果。

EvidenciaItemVideModel.cs

private string caption;
private ImageSource thumb;     

public ImageSource Thumb
{
    get { return this.thumb; }
    set { SetValue(ref this.thumb, value); }
}

public string Caption
{
    get { return this.caption; }
    set { SetValue(ref this.caption, value); }
}

Listview与以下属性关联。

private ObservableCollection<EvidenciaItemViewModel> evidencias;
public ObservableCollection<EvidenciaItemViewModel> Evidencias
{
    get { return this.evidencias; }
    set { SetValue(ref this.evidencias, value); }
}

我在列表视图中添加项目的片段

    if (this.file != null)
    {
        this.Evidencias.Add(new EvidenciaItemViewModel {
            Caption = "Archivo " + (this.Evidencias.Count + 1),
            Thumb = this.ImageSource
        });                
    }

删除项目的过程如下。

public void RemoveEvidencia(EvidenciaItemViewModel obj)
{
    this.Evidencias.Remove(obj);
}

一切正常,删除项目时出现问题;例如:如果我有4个项目并且消除了数字2,则将复制最后一个元素的图像,直到消除的下一个元素的位置。我添加了一些图像以显示我的指示。

ListView loaded before removing item

Item removed, the image of the last element is replicated in the ones that follow it after the deleted element.

1 个答案:

答案 0 :(得分:0)

删除按钮的点击方法应如下所示:

void OnDeleteClicked(Object sender, MyEventArgs args)
{
  Button btn = sender as Button;
  var evidenciaItemVideModel = btn.BindingContext as EvidenciaItemVideModel;
  EvidenciaVideModelInstance.RemoveEvidencia(evidenciaItemVideModel);
}

然后EvidenciaVideModel的RemoveEvidencia可以工作:

public void RemoveEvidencia(EvidenciaItemViewModel obj)
{
   Evidencias.Remove(obj);
}