Xamarin从列表ObservableCollection和listview表单中删除项目

时间:2018-11-18 14:58:55

标签: c# xamarin mobile

我在Xamarin上有一个简单的应用程序(待办事项列表),目的是动态创建列表中的项目并从列表中删除项目。我使用ObservableCollection作为列表。我花了很多时间对此进行研究,但无法使其正常工作。

现在,我的应用程序可以将项目添加到列表中,并在主窗体中显示它。现在,我希望它通过单击按钮从列表中删除相应的项目。

这是我的代码:

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App3"
             x:Class="App3.MainPage">

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add" Clicked="addnewitem"/>
    </ContentPage.ToolbarItems>

    <ContentPage.BindingContext>
        <local:viewmod/>
    </ContentPage.BindingContext>

    <StackLayout>
        <Editor x:Name="txtboxNAME"></Editor>
    <ListView ItemsSource="{Binding Tasks}" HasUnevenRows="True" x:Name="itemListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <ViewCell>
                        <Frame>
                            <StackLayout>
                                <Editor Text="{Binding Taskname}"/>
                                <Switch/>
                                <Button Text="Delete" CommandParameter="{Binding ItemName}" Clicked="DeleteClicked">
                                </Button>
                            </StackLayout>
                        </Frame>
                    </ViewCell>
                </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>
</ContentPage>

MainPage.xaml.cs (MainPage表单后面的代码)

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App3
{
    public partial class MainPage : ContentPage
    {
        static int itemid = 0;  

        public MainPage()
        {
            InitializeComponent();
            BindingContext = new viewmod();
        }

        private void addnewitem(object sender, EventArgs e)
        {
            var vm = BindingContext as viewmod;
            string itemnameval = "item_" + itemid.ToString();
            vm.AddItems(this.txtboxNAME.Text, itemnameval);
            itemid++;
        }

        private void DeleteClicked(object sender, EventArgs e) // Item should be deleted from the list
        {
            // This does not work
            var itemsender = (Xamarin.Forms.Button)sender;
            var item = itemsender?.BindingContext as Task;
            var vm = BindingContext as viewmod;
            vm?.RemoveCommand.Execute(item);
            //vm.Tasks.Remove(item); // conversion error

            // This does not work either. "allItems" is not defined.
            TaskClass listitem = (from itm in allItems
                             where itm.ItemName == item.CommandParameter.ToString()
                             select itm).FirstOrDefault<TaskClass>();
            allItems.Remove(listitem);
        }

    }
}

TaskClass.cs

namespace App3
{
    class TaskClass
    {
        public string Taskname { get; set; }
        public string ItemName { get; set; }
    }
}

viewmod.cs

using System.Collections.ObjectModel;
using Xamarin.Forms;

namespace App3
{
    class viewmod
    {
        public ObservableCollection<TaskClass> Tasks { get; set; } = new ObservableCollection<TaskClass>();

        public viewmod()
        {
        }

        public void AddItems(string taskn, string taskid)
        {
            Tasks.Add(new TaskClass { Taskname = $"{taskn}", ItemName=$"{taskid}" });
        }

        public void DelItem(TaskClass task)
        {
            Tasks.Remove(task);
        }

        public Command<TaskClass> RemoveCommand
        {
            get
            {
                return new Command<TaskClass>((task) => 
                {
                    Tasks.Remove(task);
                });
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

首先修改XAML =“”。语法传递整个绑定对象

<Button Text="Delete" CommandParameter="{Binding .}" Clicked="DeleteClicked" />

然后在代码后面

   private void DeleteClicked(object sender, EventArgs e)
   {
        var itemsender = (Xamarin.Forms.Button)sender;
        var item = (TaskClass)itemsender?.CommandParameter;
        // it would be much cleaner to keep a ref to your VM in your page
        // rather than continually casting it from BindingContext
        var vm = BindingContext as viewmod;
        vm.Tasks.Remove(item); 
   }