简体   繁体   English

为什么从绑定列表中删除对象后 Xamarin 中 ListView 上的项目不会消失,即使 ItemSource 属性已重置?

[英]Why items on ListView in Xamarin do not disappear after object is removed from binded list, even if ItemSource property is reset?

I'm currently learning Xamarin and are at the stage of examining ListView.我目前正在学习 Xamarin 并且处于检查 ListView 的阶段。 I've noticed that if I bind ListView to a List and remove an item from it, the listview will not display this change.我注意到如果我将 ListView 绑定到一个 List 并从中删除一个项目,listview 将不会显示此更改。 I get it that I need to use ObservableCollection to have it work automatically (or have a collection implement proper interface), but I just would like to understand why doesn't it work even when i reset the ItemsSource property of ListView after the removal.我知道我需要使用 ObservableCollection 让它自动工作(或让集合实现正确的接口),但我只是想了解为什么即使我在删除后重置 ListView 的 ItemsSource 属性它也不起作用。 Here's the code:这是代码:

 namespace Lists
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class ListsOne : ContentPage
    {
       List<Contact> _contacts = new List<Contact> {
                    new Contact{ Name = "Mosh", Number = "1234566", ImageUrl="http://lorempixel.com/100/100/people/1"},
                    new Contact { Name = "Josh", Number = "1236578" , ImageUrl = "http://lorempixel.com/100/100/people/2"}};

        IEnumerable<Contact> GetContacts(string searchText = null)
        {  

            if (String.IsNullOrWhiteSpace(searchText))
            {
                return _contacts;
            }

            return _contacts.Where(c => c.Name.StartsWith(searchText));
        }

        public ListsOne()
        {
            InitializeComponent();            
            listView.ItemsSource = GetContacts();
        }       

        private void Delete_Clicked(object sender, EventArgs e)
        {
            Contact contact = (sender as MenuItem).CommandParameter as Contact;
            _contacts.Remove(contact);
            listView.ItemsSource = GetContacts();

        }

        private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            listView.ItemsSource = GetContacts(e.NewTextValue);
        }
    }
}

As you can see in the Delete_Clicked method, after removing contact from _contacts, I reset the ItemsSource, but it will have no effect on the app, even though the pretty much same implementation for SearchBar_TextChanged works (if I recall correctly, am at work right now).正如您在 Delete_Clicked 方法中看到的,从 _contacts 中删除联系人后,我重置了 ItemsSource,但它不会对应用程序产生任何影响,即使 SearchBar_TextChanged 的​​实现几乎相同(如果我没记错的话,我在工作现在)。 Any insight on how it works?关于它是如何工作的任何见解? Sorry if it's dumb, but I am just learning.对不起,如果它很愚蠢,但我只是在学习。

Replace List with ObservableCollection.用 ObservableCollection 替换列表。 Latter one has event that notifies the UI about changes in it's array.后一个事件通知 UI 其数组中的更改。 (This is when you use MVVM, might not be applicable in in your example). (这是当您使用 MVVM 时,可能不适用于您的示例)。

Also, as far as I know there is an issue with ListView.ItemSource when adding removing items to it.此外,据我所知,向 ListView.ItemSource 添加删除项目时存在问题。 To make it work do this:要使其工作,请执行以下操作:

        private void Delete_Clicked(object sender, EventArgs e)
    {
        Contact contact = (sender as MenuItem).CommandParameter as Contact;
        _contacts.Remove(contact);
        listView.ItemsSource = null;
        listView.ItemsSource = GetContacts();

    }

In the document of listview-ItemsSource , it says:listview-ItemsSource的文档中,它说:

If you want the ListView to automatically update as items are added, removed and changed in the underlying list, you'll need to use an ObservableCollection.如果您希望 ListView 在基础列表中添加、删除和更改项目时自动更新,则需要使用 ObservableCollection。 ObservableCollection is defined in System.Collections.ObjectModel and is just like List, except that it can notify ListView of any changes: ObservableCollection是在 System.Collections.ObjectModel 中定义的,和 List 一样,除了它可以通知 ListView 任何变化:

ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
listView.ItemsSource = employees;

//Mr. Mono will be added to the ListView because it uses an ObservableCollection
employees.Add(new Employee(){ DisplayName="Mr. Mono"});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM