我正在构建一个Xamarin跨平台应用程序!
该应用程序包含2个页面:HomePage(主页)
和DetailGetData(获取详细数据)
HomePage:
该页面包含一个ListView,其中显示来自WebApi的数据列表。每当我单击每个单元格时,它都会转到DetailGetData
页面,显示该数据的详细信息。
问题:
现在的问题是,我想从DetailGetData
页面删除所选项。放置了DeleteButton(删除按钮)
,当我按下该按钮时,列表中的详细信息和所选项都将被删除。
如何实现?
DetailGetData屏幕截图:https://istack.dev59.com/TXg4G.webp
HomePage屏幕截图:https://istack.dev59.com/g1Hn1.webp
代码:
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>
主页 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.HomePage">
<AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<ListView x:Name="Demolist" ItemSelected="OnItemSelected" BackgroundColor="White">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell Height="100">
<ViewCell.ContextActions>
<MenuItem x:Name="OnMore" Clicked="OnMore_Clicked" CommandParameter="{Binding .}"
Text="More" />
<MenuItem x:Name="OnDelete" Clicked="OnDelete_Clicked" CommandParameter="{Binding .}"
Text="Delete" IsDestructive="True" />
</ViewCell.ContextActions>
<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" Padding="30,0">
<Label Text="{Binding employee_name}" FontAttributes="bold" FontSize="Small" TextColor="Black" x:Name="en"/>
<Label Text="{Binding employee_age}" FontSize="Micro" TextColor="Black" FontAttributes="Italic"/>
<Label Text="{Binding id}" IsVisible="False" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ImageButton Source="fedit.png"
BackgroundColor="Transparent"
AbsoluteLayout.LayoutFlags="PositionProportional"
AbsoluteLayout.LayoutBounds=".95,.95,55,55"
Clicked="ImageButton_Clicked">
</ImageButton>
</AbsoluteLayout>
</ContentPage>
HomePage.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Last_MSPL.MenuItems;
using Last_MSPL.Views;
using System.Collections;
namespace Last_MSPL
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class HomePage : ContentPage
{
public IEnumerable ObjOrderList { get; private set; }
public HomePage()
{
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Black;
InitializeComponent();
Get();
}
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;
Demolist.ItemsSource = ObjOrderList.GetRange(0, 40);
}
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);
}
}
DetailGetData.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Last_MSPL.MenuItems;
namespace Last_MSPL.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DetailGetData : ContentPage
{
public DetailGetData(GetData _data)
{
InitializeComponent();
BindingList(_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)
{
await Navigation.PopModalAsync();
}
}
}