c# - Xamarin 表单中的多个列表数据检索
问题描述
Person | fruits | vegetables
______________________________________________
P1 | Apple Banana | tomato, spinach
P2 | Orange Apple | Onion, Garlic
我正在学习一些概念。我有上述格式的数据。有人可以建议在这种情况下我可以使用哪种数据结构来检索数据。还有人可以告诉我如何在滚动视图中有多个列表吗?我可以通过在 app.xaml 中定义设计(用于水果、蔬菜列表)来使用通用模板吗?如何使用从上表中检索到的数据填充到我的 ScrollViewList 中?
解决方案
数据结构
public class Goods
{
public string Name { get; set; }
}
public class GoodsList : List<Goods>
{
public string Name { get; set; }
public List<Goods> GoodsAll => this;
}
public class Person
{
public string Name { get; set; }
public List<GoodsList> List;
}
您需要两个页面,分别称为PersonPage
和GoodsPage
。
个人页面
//Xaml
<ListView ItemsSource="{Binding list}" ItemSelected="ListView_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
//Code
public partial class Page1 : ContentPage
{
public List<Person> list { get; set; }
public Page1()
{
InitializeComponent();
var list1 = new GoodsList() { new Goods { Name = "Apple" } , new Goods { Name = "Banana" } };
list1.Name = "Fruits";
var list2 = new GoodsList() { new Goods { Name = "Tomato" }, new Goods { Name = "Spinach" } };
list2.Name = "Vegetables";
var list3 = new GoodsList() { new Goods { Name = "Apple" }, new Goods { Name = "Orange" } };
list3.Name = "Fruits";
var list4 = new GoodsList() { new Goods { Name = "Onion" }, new Goods { Name = "Garlic" } };
list4.Name = "Vegetables";
list = new List<Person>();
list.Add(new Person
{
Name = "P1",
List = new List<GoodsList>() { list1,list2}
}) ;
list.Add(new Person
{
Name = "P2",
List = new List<GoodsList>() { list3, list4}
});
this.BindingContext = this;
}
private void ListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
this.Navigation.PushAsync(new Page2(e.SelectedItem), true) ;
}
}
商品页面
//Xaml
<ListView x:Name="listview" IsGroupingEnabled="true">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label BackgroundColor="Pink"
HorizontalOptions="FillAndExpand"
Text="{Binding Name}" />
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label HorizontalTextAlignment="Center"
FontSize="Large"
HorizontalOptions="FillAndExpand"
Text="{Binding Name}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
//Code
public partial class Page2 : ContentPage
{
public Page2(object obj)
{
InitializeComponent();
var person = obj as Person;
listview.ItemsSource = person.List;
}
}
推荐阅读
- python - 从数据集中提取数据组 - Python
- python - Tkinter 主循环的预期流程是什么?Tkinter 正在冻结算法的时间流
- javascript - MUI 数据表。按名称而不是索引访问 rowData 字段
- delphi - 如何修复单行程序中的 TWebBrowser 内存泄漏?德尔福 10.3
- firebase - 一旦你在一个事件上调用了 dispose(),它就不能再使用了,因为有多个 QuerySnapshots
- reactjs - 使用 formik 单击新的选择下拉菜单后 Recat 复选框
- pandas - 如何从 Pandas 数据框中删除包含空单元格但值为 0 的行?
- php - 在 WooCommerce 管理订单页面中保留和显示产品自定义字段值
- flask-restplus - 如何在 flask-restplus/flask-restx 的有效负载中指定通用字典?
- excel - VBA | 如何计算不同值的出现次数?