簡體   English   中英

ListView不會刪除Xamarin.Forms中的項目。 我已將ObservableCollection分配給ListView itemsource。 MVVM

[英]ListView not deleting items in Xamarin.Forms. I have assigned an ObservableCollection to the ListView itemsource. MVVM

在我的Xamarin.Forms應用程序中,我有一個簡單的Contact類[Model]。 在UI [視圖]中,存在一個顯示聯系人的ListView。 在我的模型視圖類中,我有一個聯系人列表(_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; }
}

模型視圖

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>

contactList.ItemsSource = _listOfContacts.ToList();刪除.toList() contactList.ItemsSource = _listOfContacts.ToList(); 然后再試一次。

_listOfContacts是一個ObservableCollection ,應直接用作您的ItemsSource 也許去看看ObservableCollection文檔

我已經測試了您的代碼,只是導致此問題的方法ToList()

 contactList.ItemsSource = _listOfContacts.ToList();

一開始, _listOfContacts的類型是ObservableCollection,但是當您使用方法ToList() ,它將再次轉換為List 因此,只需刪除方法“ ToList()”,您的代碼即可正常工作,如下所示:

contactList.ItemsSource = _listOfContacts;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM