作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建 Xamarin CrossPlatform 应用程序!
应用包含 2 个页面:HomePage
、DetailGetData
首页:此页面包含一个 ListView,它在单元格中显示来自 webapi 的数据列表,每当我单击每个单元格时,它都会转到显示该数据详细信息的 DetailGetData
页面。
问题:现在的问题是我想从 DetailGetData
页面中删除所选项目。放置了一个 DeleteButton
,当我按下该按钮时,详细信息和所选项目也应该从 ListView 中删除。这怎么可能?
屏幕截图详细信息获取数据: /image/TXg4G.png
首页截图:/image/g1Hn1.png
代码:
详细获取数据 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>
首页.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();
}
}
}
最佳答案
可以通过添加静态datasouce类实现删除item的功能。并设置 Demolist.ItemsSource = DataSource.collection;
当点击 DetailGetData 页面中的delete
按钮时,通过删除项目来修改 Demolist.ItemsSource
。所以代码是这样的:
数据源.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);
}
}
}
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);
}
}
}
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();
}
}
关于xaml - 如何在 Xamarin 中删除从 ListView 中选择的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54673209/
我是一名优秀的程序员,十分优秀!