C# Xamarin表单中的多列表数据检索
我学的概念很少。我有上述格式的数据。有人能建议我在本例中检索数据时使用的最佳数据结构吗。 还有人能告诉我如何在一个滚动视图中有多个列表吗?我可以通过在app.xaml(水果、蔬菜列表)中定义设计来使用公共模板吗?如何使用上表中检索到的数据填充我的ScrollViewList 我的滚动视图应该如下所示: 数据结构 GoodsPageC# Xamarin表单中的多列表数据检索,c#,xamarin,xamarin.forms,xamarin.android,C#,Xamarin,Xamarin.forms,Xamarin.android,我学的概念很少。我有上述格式的数据。有人能建议我在本例中检索数据时使用的最佳数据结构吗。 还有人能告诉我如何在一个滚动视图中有多个列表吗?我可以通过在app.xaml(水果、蔬菜列表)中定义设计来使用公共模板吗?如何使用上表中检索到的数据填充我的ScrollViewList 我的滚动视图应该如下所示: 数据结构 GoodsPage //Xaml //代码 公共部分类第2页:内容页 { 公共页面2(对象对象对象) { 初始化组件(); var person=obj as person; listv
//Xaml
//代码
公共部分类第2页:内容页
{
公共页面2(对象对象对象)
{
初始化组件();
var person=obj as person;
listview.ItemsSource=person.List;
}
}
您需要的是:具有MultiSelect的CollectionView、CollectionView GroupHeaderTemplate和与您的单元格相似的ContentView!如有疑问,请随时返回
Person | fruits | vegetables
______________________________________________
P1 | Apple Banana | tomato, spinach
P2 | Orange Apple | Onion, Garlic
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;
}
//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;
}
}