[英]ListView not deleting items in Xamarin.Forms. I have assigned an ObservableCollection to the ListView itemsource. MVVM
In my Xamarin.Forms app I have a simple Contact class [Model]. 在我的Xamarin.Forms应用程序中,我有一个简单的Contact类[Model]。 In the UI [View] there exists a ListView that displays the Contacts.
在UI [视图]中,存在一个显示联系人的ListView。 In my model view class I have a list of Contacts (_listOfContacts) that is assigned to the itemSource property of the ListView.
在我的模型视图类中,我有一个联系人列表(_listOfContacts),该列表分配给ListView的itemSource属性。 This list of Contacts is an ObservableCollection.
此联系人列表是一个ObservableCollection。 My issue is the when user clicks Delete from ContextActions I can see that the _listOfContacts is updated but the ListView is not.
我的问题是当用户单击ContextActions中的Delete时,我可以看到_listOfContacts已更新,但ListView没有更新。 The ListView is only updated when I reassign its itemsource to the _listOfContacts.
仅当我将其itemsource重新分配给_listOfContacts时,才会更新ListView。 This should not be needed if _listOfContacts is an ObservableCollection of Contacts.
如果_listOfContacts是Contacts的ObservableCollection,则不需要这样做。 I am new to MVVM so I need to clear these basic MVVM concepts before I go on to learn more advanced techniques.
我是MVVM的新手,因此在继续学习更高级的技术之前,需要清除这些基本的MVVM概念。 Here is my code:
这是我的代码:
Model 模型
class Contact
{
public String Name { get; set; }
public String Status { get; set; }
public String ImageUrl { get; set; }
}
Model View 模型视图
public partial class ContactListPage : ContentPage
{
private ObservableCollection<Contact> _listOfContacts;
public ContactListPage()
{
InitializeComponent();
_listOfContacts = new ObservableCollection<Contact>
{
new Contact {Name="Item1", ImageUrl="http://lorempixel.com/100/100/people/1" , Status="Hey"},
new Contact { Name = "Item2", ImageUrl = "http://lorempixel.com/100/100/people/2", Status="Hey" },
};
contactList.ItemsSource = _listOfContacts.ToList();
}
private void EditContactClick(object sender, EventArgs e)
{
DisplayAlert("Alert", "Clicked Edit", "Cancel");
}
private void DeleteContactClick(object sender, EventArgs e)
{
var contact = (sender as MenuItem).CommandParameter as Contact;
_listOfContacts.Remove(contact);
//following line of code should not be needed since _listOfContacts is
//an ObservableCollection and removing an item should update the bound control automatically
**contactList.ItemsSource = _listOfContacts.ToList();**
}
}
View 视图
<ContentPage.Content>
<StackLayout>
<ListView x:Name="contactList" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="10">
<Image Source="{Binding ImageUrl}"/>
<StackLayout HorizontalOptions="StartAndExpand">
<Label Text="{Binding Name}" Margin="0,2,0,2"/>
<Label Text="{Binding Status}" Margin="0,2,0,2" />
</StackLayout>
</StackLayout>
<ViewCell.ContextActions>
<MenuItem Text="Edit" Clicked="EditContactClick" CommandParameter="{Binding .}"/>
<MenuItem Text="Delete" Clicked="DeleteContactClick" IsDestructive="True" CommandParameter="{Binding .}"/>
</ViewCell.ContextActions>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
Remove .toList()
from contactList.ItemsSource = _listOfContacts.ToList();
从
contactList.ItemsSource = _listOfContacts.ToList();
删除.toList()
contactList.ItemsSource = _listOfContacts.ToList();
and try again. 然后再试一次。
_listOfContacts
is an ObservableCollection
which should be used as your ItemsSource
directly. _listOfContacts
是一个ObservableCollection
,应直接用作您的ItemsSource
。 Maybe go and check out the ObservableCollection documentation . 也许去看看ObservableCollection文档 。
I have tested your code, it is just the method ToList()
which caused this question: 我已经测试了您的代码,只是导致此问题的方法
ToList()
:
contactList.ItemsSource = _listOfContacts.ToList();
At the beginning, the type of _listOfContacts
is ObservableCollection, but when you use the method ToList()
,then it will been converted to List
again. 一开始,
_listOfContacts
的类型是ObservableCollection,但是当您使用方法ToList()
,它将再次转换为List
。 So just delete the method 'ToList()', your code will work properly, just as follows: 因此,只需删除方法“ ToList()”,您的代码即可正常工作,如下所示:
contactList.ItemsSource = _listOfContacts;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.