Xaml 如何在Xamarin中从ListView中删除选中的项目

Xaml 如何在Xamarin中从ListView中删除选中的项目,xaml,listview,button,xamarin.forms,cross-platform,Xaml,Listview,Button,Xamarin.forms,Cross Platform,我正在构建一个Xamarin跨平台应用程序 应用程序包含两个页面:主页,详细信息数据 主页: 此页面包含一个ListView,它在单元格中显示数据表单webapi的列表,每当我单击每个单元格时,它都会转到显示该数据详细信息的DetailGetData页面 问题: 现在的问题是,我想从DetailGetData页面删除所选项目。将放置一个delete按钮,当我按下该按钮时,详细信息和所选项目也应从列表视图中删除。 这是怎么可能的? 屏幕截图详细信息获取数据: 屏幕截图主页: 代码: DetailG

我正在构建一个Xamarin跨平台应用程序

应用程序包含两个页面:
主页
详细信息数据

主页: 此页面包含一个ListView,它在单元格中显示数据表单webapi的列表,每当我单击每个单元格时,它都会转到显示该数据详细信息的
DetailGetData
页面

问题: 现在的问题是,我想从
DetailGetData
页面删除所选项目。将放置一个
delete按钮
,当我按下该按钮时,详细信息和所选项目也应从列表视图中删除。 这是怎么可能的?

屏幕截图详细信息获取数据:

屏幕截图主页

代码:

DetailGetData 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"
             x:Class="Last_MSPL.Views.DetailGetData">

    <StackLayout HorizontalOptions="Center" VerticalOptions="Center">

        <Label Text="{Binding employee_name}" x:Name="empname" FontSize="Medium" FontAttributes="Bold" />
        <Label Text="{Binding employee_age}" x:Name="age" FontSize="Medium" FontAttributes="Bold" />
        <Label Text="{Binding employee_salary}" x:Name="salary" FontSize="Medium" FontAttributes="Bold" />

        <Button x:Name="DeleteItem" Text="Delete" Clicked="DeleteItem_Clicked"  />
    </StackLayout>

</ContentPage>

在某个类中初始化列表,使其成为静态,并在主页中使用它。然后在“删除事件”的详细信息页面中,只需编写以下代码即可从列表中删除该项

var remove = list.Where(x => x.employee_name == empname.Text).Single();
list.Remove(remove);

根据模型类更改代码。希望对您有所帮助。

您可以通过添加静态datasouce类来实现删除项目的功能。并设置
Demolist.ItemsSource=DataSource.collection
单击DetailGetData页面中的
delete
按钮时,通过删除项目修改
Demolist.ItemsSource
。 所以代码是这样的:

DataSource.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace App10
{
    public static class DataSource
    {
        public static ObservableCollection<GetData> collection;

        static DataSource()
        {
        }
        public static void persist(List<GetData> collection)
        {
            //do something here
        }

        public static void initializeData(List<GetData> listdata)
        {
            collection = new ObservableCollection<GetData>(listdata);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统文本;
名称空间App10
{
公共静态类数据源
{
公共静态可观测集合;
静态数据源()
{
}
公共静态void persist(列表集合)
{
//在这里做点什么
}
公共静态无效初始化数据(列表数据)
{
收集=新的ObservableCollection(listdata);
}
}
}
MainPage.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage
    {
        List<GetData> dataList;
        public MainPage()
        {
            //((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Black;
            InitializeComponent();
            Get();
            //RefreshList();
        }
        public async void Get()
        {
            HttpClient client = new HttpClient();
            try
            {
                var respone = await client.GetStringAsync("http://dummy.restapiexample.com/api/v1/employees");
                List<GetData> ObjOrderList = JsonConvert.DeserializeObject<List<GetData>>(respone);
                var totalCount = ObjOrderList.Count;

                dataList = ObjOrderList.GetRange(0, 40);
                DataSource.initializeData(dataList);
                Demolist.ItemsSource = DataSource.collection;

            }
            catch (Exception ex)
            {
                throw;
            }
        }


        public async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            HttpClient client = new HttpClient();
            if (Demolist.SelectedItem != null)
            {

                var respone = await client.GetStringAsync("http://dummy.restapiexample.com/api/v1/employees");
                List<GetData> ObjOrderList = JsonConvert.DeserializeObject<List<GetData>>(respone);
                var abc = (GetData)e.SelectedItem;

                GetData data = new GetData();
                data = ObjOrderList.ToList().Where(x => x.id == abc.id).FirstOrDefault();

                var detailPage = new DetailGetData(data);
                detailPage.BindingContext = e.SelectedItem as GetData;
                Demolist.SelectedItem = null;
                await Navigation.PushModalAsync(detailPage);

            }
        }
}
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类主页:ContentPage
{
列表数据列表;
公共主页()
{
//((NavigationPage)Application.Current.MainPage).BarBackgroundColor=Color.Black;
初始化组件();
Get();
//刷新列表();
}
公共异步void Get()
{
HttpClient=新的HttpClient();
尝试
{
var respone=await client.GetStringAsync(“http://dummy.restapiexample.com/api/v1/employees");
List ObjOrderList=JsonConvert.DeserializeObject(respone);
var totalCount=ObjOrderList.Count;
dataList=ObjOrderList.GetRange(0,40);
DataSource.initializeData(数据列表);
Demolist.ItemsSource=DataSource.collection;
}
捕获(例外情况除外)
{
投掷;
}
}
已选择公共异步void(对象发送方,SelectedItemChangedEventArgs e)
{
HttpClient=新的HttpClient();
如果(Demolist.SelectedItem!=null)
{
var respone=await client.GetStringAsync(“http://dummy.restapiexample.com/api/v1/employees");
List ObjOrderList=JsonConvert.DeserializeObject(respone);
var abc=(GetData)e.SelectedItem;
GetData=新的GetData();
data=ObjOrderList.ToList(),其中(x=>x.id==abc.id).FirstOrDefault();
var detailPage=新的DetailGetData(数据);
detailPage.BindingContext=e.SelectedItem作为GetData;
Demolist.SelectedItem=null;
等待导航。PushModalAsync(详情页);
}
}
}
DetailGetData.xaml.cs

   [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DetailGetData : ContentPage
    {
        public GetData thisData;

        public DetailGetData(GetData _data)
        {
            InitializeComponent();
            BindingList(_data);
            thisData = _data;

        }


        public void BindingList(GetData data)
        {
            empname.Text = data.employee_name;
            age.Text = data.employee_age;
            salary.Text = data.employee_salary;
        }


        public async void DeleteItem_Clicked(object sender, EventArgs e)
        {

            GetData toberemoveditem = (from item in DataSource.collection
                                       where item.id == thisData.id
                             select item)
                            .FirstOrDefault<GetData>();
            DataSource.collection.Remove(toberemoveditem);


            await Navigation.PopModalAsync();
        }
    }
[XamlCompilation(XamlCompilationOptions.Compile)]
公共部分类DetailGetData:ContentPage
{
公共获取数据;
公共详细信息GetData(GetData\u数据)
{
初始化组件();
绑定列表(_数据);
thisData=_数据;
}
公共void绑定列表(GetData)
{
empname.Text=data.employee\u name;
age.Text=data.employee\u年龄;
salary.Text=data.employee\u salary;
}
已单击公共异步void DeleteItem(对象发送方,事件参数e)
{
GetData toberemoveditem=(来自DataSource.collection中的项
其中item.id==thisData.id
选择项目)
.FirstOrDefault();
DataSource.collection.Remove(toberemoveditem);
等待导航。PopModalAsync();
}
}

将您的c#代码也放在列表的起始位置。@wasifmoodmustafa添加。如果你能帮我解决这个问题,现在请检查一下!发起一个事件或使用MessagingCenter告诉您的主页需要删除某个项目deleted@JasonDeleteItem_Clicked事件在我的代码中,由一个按钮引发,但我不知道这样做的逻辑!不,您的详细信息页面应该会引发一个“已删除”事件,如果您能提供帮助,您的主页将订阅更多内容。这只适用于运行时,如果我想从我的webapi中删除,那么我要做什么?您可以使用删除url“”上的数据来从webapi中删除。请参阅
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace App10
{
    public static class DataSource
    {
        public static ObservableCollection<GetData> collection;

        static DataSource()
        {
        }
        public static void persist(List<GetData> collection)
        {
            //do something here
        }

        public static void initializeData(List<GetData> listdata)
        {
            collection = new ObservableCollection<GetData>(listdata);
        }
    }
}
[XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MainPage : ContentPage
    {
        List<GetData> dataList;
        public MainPage()
        {
            //((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Black;
            InitializeComponent();
            Get();
            //RefreshList();
        }
        public async void Get()
        {
            HttpClient client = new HttpClient();
            try
            {
                var respone = await client.GetStringAsync("http://dummy.restapiexample.com/api/v1/employees");
                List<GetData> ObjOrderList = JsonConvert.DeserializeObject<List<GetData>>(respone);
                var totalCount = ObjOrderList.Count;

                dataList = ObjOrderList.GetRange(0, 40);
                DataSource.initializeData(dataList);
                Demolist.ItemsSource = DataSource.collection;

            }
            catch (Exception ex)
            {
                throw;
            }
        }


        public async void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            HttpClient client = new HttpClient();
            if (Demolist.SelectedItem != null)
            {

                var respone = await client.GetStringAsync("http://dummy.restapiexample.com/api/v1/employees");
                List<GetData> ObjOrderList = JsonConvert.DeserializeObject<List<GetData>>(respone);
                var abc = (GetData)e.SelectedItem;

                GetData data = new GetData();
                data = ObjOrderList.ToList().Where(x => x.id == abc.id).FirstOrDefault();

                var detailPage = new DetailGetData(data);
                detailPage.BindingContext = e.SelectedItem as GetData;
                Demolist.SelectedItem = null;
                await Navigation.PushModalAsync(detailPage);

            }
        }
}
   [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class DetailGetData : ContentPage
    {
        public GetData thisData;

        public DetailGetData(GetData _data)
        {
            InitializeComponent();
            BindingList(_data);
            thisData = _data;

        }


        public void BindingList(GetData data)
        {
            empname.Text = data.employee_name;
            age.Text = data.employee_age;
            salary.Text = data.employee_salary;
        }


        public async void DeleteItem_Clicked(object sender, EventArgs e)
        {

            GetData toberemoveditem = (from item in DataSource.collection
                                       where item.id == thisData.id
                             select item)
                            .FirstOrDefault<GetData>();
            DataSource.collection.Remove(toberemoveditem);


            await Navigation.PopModalAsync();
        }
    }