c# - ListView不会删除Xamarin.Forms中的项目。我已将ObservableCollection分配给ListView itemsource。 MVVM

标签 c# listview mvvm xamarin.forms observablecollection

在我的Xamarin.Forms应用程序中,我有一个简单的Contact类[Model]。在UI [ View ]中,存在一个显示联系人的ListView。在模型 View 类中,我有一个联系人列表(_listOfContacts),该列表分配给ListView的itemSource属性。此联系人列表是一个ObservableCollection。我的问题是,当用户单击ContextActions中的Delete时,我可以看到_listOfContacts已更新,但ListView没有更新。
仅当我将其itemsource重新分配给_listOfContacts时,才会更新ListView。如果_listOfContacts是Contacts的ObservableCollection,则不需要这样做。
我是MVVM的新手,因此在继续学习更高级的技术之前,需要清除这些基本的MVVM概念。
这是我的代码:

模型

 class Contact
{
    public String Name { get; set; }
    public String Status { get; set; }
    public String ImageUrl { get; set; }
}

模型 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();**
    }
}

看法
<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>

最佳答案

我已经测试过您的代码,只是ToList()方法引起了这个问题:

 contactList.ItemsSource = _listOfContacts.ToList();

一开始,_listOfContacts的类型是ObservableCollection,但是当您使用ToList()方法时,它将再次转换为List
因此,只需删除方法“ToList()”,您的代码即可正常工作,如下所示:
contactList.ItemsSource = _listOfContacts;

关于c# - ListView不会删除Xamarin.Forms中的项目。我已将ObservableCollection分配给ListView itemsource。 MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57403867/

相关文章:

c# - 返回 ViewModel 而不是 Model

android - 奇怪的 ClassCastException : ImageView cannot be cast to ListView

xaml - 更改 ListView 项目的颜色并删除项目 UWP

flutter - ListView 内列未正确展开

google-maps - MVVM模式(Prism)和Xamarin映射

c# - 使用 TaskCompletionSource.TrySetResult() 时出错

c# - 缺少 ServiceStack 的 DLL

c# - 显式地将基类转换为派生类

c# - 如何使 CM View 显示两个 subview 之一,所有(包括父)都绑定(bind)到同一个 View 模型?

c# - MVVM 按钮无法按下