Xamarin.forms Xamarin表单:Listview分组问题
我正在尝试为下面的JSON数据实现listview分组 JSON示例:Xamarin.forms Xamarin表单:Listview分组问题,xamarin.forms,listviewgroup,Xamarin.forms,Listviewgroup,我正在尝试为下面的JSON数据实现listview分组 JSON示例: { "cbrainBibleBooksHB":[ { "book":"2 John", "cbrainBibleTOList":[ { "bookName":"2 John", "chapter":"1", "pageUrl":"/edu-bible/9005/1/
{
"cbrainBibleBooksHB":[ {
"book":"2 John",
"cbrainBibleTOList":[
{
"bookName":"2 John",
"chapter":"1",
"pageUrl":"/edu-bible/9005/1/2-john-1"
},
{....}
]
},
{
"book":"3 John",
"cbrainBibleTOList":[
{
"bookName":"3 John",
"chapter":"1",
"pageUrl":"/edu-bible/9007/1/3-john-1"
},
{...}
]
}
]
}
我正试图按其书名对JSON数据进行分组
我试过如下:
型号:
public class BibleTestament
{
public List<CbrainBibleBooksHB> cbrainBibleBooksHB { get; set; }
}
public class CbrainBibleBooksHB : ObservableCollection<CbrainBibleTOList>
{
public string book { get; set; }
public List<CbrainBibleTOList> cbrainBibleTOList { get; set; }
}
public class CbrainBibleTOList
{
public string chapter { get; set; }
public string pageUrl { get; set; }
public string bookName { get; set; }
}
<ScrollView>
<FlexLayout
BindableLayout.ItemsSource="{Binding AllItems}"
Direction="Column"
AlignContent="Start">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0"
Text="{Binding book}"
HorizontalOptions="FillAndExpand"
BackgroundColor="LightBlue"/>
<StackLayout Grid.Row="1"
BindableLayout.ItemsSource="{Binding cbrainBibleTOList}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding chapter}">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.TapCommand, Source={x:Reference Name=ParentContentPage}}" CommandParameter="{Binding .}"/>
</Label.GestureRecognizers>
</Label>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>
</ScrollView>
class BibleTestamentViewModel : INotifyPropertyChanged
{
public ICommand TapCommand { get; private set; }
public BibleTestamentViewModel()
{
TapCommand = new Command(ChapterClickedClicked);
}
private void ChapterClickedClicked(object sender)
{
//check value inside sender
}
}
公共类圣经遗嘱
{
公共列表cbrainbiblebooksb{get;set;}
}
公共类CBrainBibleBooksB:ObservableCollection
{
公共字符串书{get;set;}
公共列表cbrainBibleTOList{get;set;}
}
公共类CbrainBibleTOList
{
公共字符串章节{get;set;}
公共字符串pageUrl{get;set;}
公共字符串bookName{get;set;}
}
视图模型
HttpClient client = new HttpClient();
var Response = await client.GetAsync("rest api");
if (Response.IsSuccessStatusCode)
{
string response = await Response.Content.ReadAsStringAsync();
Debug.WriteLine("response:>>" + response);
BibleTestament bibleTestament = new BibleTestament();
if (response != "")
{
bibleTestament = JsonConvert.DeserializeObject<BibleTestament>(response.ToString());
}
AllItems = new ObservableCollection<CbrainBibleBooksHB>(bibleTestament.cbrainBibleBooksHB);
HttpClient=newhttpclient();
var Response=await client.GetAsync(“RESTAPI”);
if(响应。IsSuccessStatusCode)
{
string response=wait response.Content.ReadAsStringAsync();
Debug.WriteLine(“响应:>>”+响应);
圣经遗嘱圣经遗嘱=新圣经遗嘱();
如果(响应!=“”)
{
BibleTestate=JsonConvert.DeserializeObject(response.ToString());
}
AllItems=新的可观察集合(bibleTestament.cbrainBibleBooksb);
XAML
<ContentPage.Content>
<StackLayout>
<ListView
HasUnevenRows="True"
ItemsSource="{Binding AllItems,Mode=TwoWay}"
IsGroupingEnabled="True">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<Label
Text="{Binding book}"
Font="Bold,20"
HorizontalOptions="CenterAndExpand"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
Margin="3"
TextColor="Black"
VerticalOptions="Center"/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout
HorizontalOptions="StartAndExpand"
VerticalOptions="FillAndExpand"
Orientation="Horizontal">
<Label
Text="{Binding cbrainBibleTOList.chapter}"
Font="20"
HorizontalTextAlignment="Center"
VerticalTextAlignment="Center"
HorizontalOptions="CenterAndExpand"
TextColor="Black"
VerticalOptions="Center"/>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<Label/>
</ListView.Footer>
</ListView>
</StackLayout>
</ContentPage.Content>
但在运行项目时,UI上没有显示任何数据。正在获取
绑定:在“System.Object[]上找不到“book”属性,目标属性:'Xamarin.Forms.Label.Text'
输出框上的消息。在Xamarin表单中实现列表视图分组非常困难。有人能帮我吗?我上载了一个示例项目。我用静态数据测试了您的演示,您的案例中存在一些问题
首先,cbrainbiblebooksb是ObservableCollection的一个子类,因此您不再需要设置属性cbrainbibletList
public class CbrainBibleBooksHB : ObservableCollection<CbrainBibleTOList>
{
public string book { get; set; }
public List<CbrainBibleTOList> cbrainBibleTOList { get; set; }
}
公共类CBrainBibleBooksB:ObservableCollection
{
公共字符串书{get;set;}
公共列表cbrainBibleTOList{get;set;}
}
其次,设置了错误的标签绑定路径
<Label
Text="{Binding chapter}"
...
/>
以下是我的代码,因为我无法访问您的url,所以我使用了静态数据
在xaml中
。。。
...
在视图模型中
命名空间测试示例
{
公共类BibleTestmentViewModel
{
公共可观测集合项目
{
获得;设置;
}
公共BibleTestamentViewModel()
{
var cbrainbiblebooksb=new cbrainbiblebooksb(){book=“group1”,};
cbrainbiblebooksb.Add(新的CbrainBibleTOList(){chapter=“1111”});
cbrainbiblebooksb.Add(新的CbrainBibleTOList(){chapter=“2222”});
cbrainbiblebooksb.Add(新的CbrainBibleTOList(){chapter=“3333”});
cbrainbiblebooksb.Add(新的CbrainBibleTOList(){chapter=“4444”});
cbrainbiblebooksb.Add(新的CbrainBibleTOList(){chapter=“5555”});
var cbrainBibleBooksHB2=新的CbrainBibleBooksHB(){book=“group2”,};
cbrainbiblebooksb2.Add(新的CbrainBibleTOList(){chapter=“6666”});
cbrainbiblebooksb2.Add(新的CbrainBibleTOList(){chapter=“7777”});
cbrainbiblebooksb2.Add(新的CbrainBibleTOList(){chapter=“8888”});
cbrainbiblebooksb2.Add(新的CbrainBibleTOList(){chapter=“9999”});
cbrainbiblebooksb2.Add(新的CbrainBibleTOList(){chapter=“0000”});
AllItems=新的可观测集合(){
cbrainbiblebooksb,cbrainbiblebooksb2
};
}
}
}
公共类CBrainBibleBooksB:ObservableCollection
{
公共字符串书{get;set;}
}
您应该确保从远程url下载的对象与我的演示具有相同的级别。您可以使用最新的Xamarin.Forms版本>=3.5,而不是使用分组列表视图,所需的工作量更少
更新您的型号课程
public class CbrainBibleBooksHB
{
public string book { get; set; }
public List<CbrainBibleTOList> cbrainBibleTOList { get; set; }
}
输出:
public class BibleTestament
{
public List<CbrainBibleBooksHB> cbrainBibleBooksHB { get; set; }
}
public class CbrainBibleBooksHB : ObservableCollection<CbrainBibleTOList>
{
public string book { get; set; }
public List<CbrainBibleTOList> cbrainBibleTOList { get; set; }
}
public class CbrainBibleTOList
{
public string chapter { get; set; }
public string pageUrl { get; set; }
public string bookName { get; set; }
}
<ScrollView>
<FlexLayout
BindableLayout.ItemsSource="{Binding AllItems}"
Direction="Column"
AlignContent="Start">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Label Grid.Row="0"
Text="{Binding book}"
HorizontalOptions="FillAndExpand"
BackgroundColor="LightBlue"/>
<StackLayout Grid.Row="1"
BindableLayout.ItemsSource="{Binding cbrainBibleTOList}">
<BindableLayout.ItemTemplate>
<DataTemplate>
<Label Text="{Binding chapter}">
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.TapCommand, Source={x:Reference Name=ParentContentPage}}" CommandParameter="{Binding .}"/>
</Label.GestureRecognizers>
</Label>
</DataTemplate>
</BindableLayout.ItemTemplate>
</StackLayout>
</Grid>
</DataTemplate>
</BindableLayout.ItemTemplate>
</FlexLayout>
</ScrollView>
class BibleTestamentViewModel : INotifyPropertyChanged
{
public ICommand TapCommand { get; private set; }
public BibleTestamentViewModel()
{
TapCommand = new Command(ChapterClickedClicked);
}
private void ChapterClickedClicked(object sender)
{
//check value inside sender
}
}
你也可以这样做
<ListView ItemsSource="{Binding Itens}" SeparatorColor="#010d47" RowHeight="120" x:Name="lvEmpresaPending" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Nome da Empresa:" FontAttributes="Bold" ></Label>
<Label Text="{Binding Main.Nome}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="CNPJ da Empresa:" FontAttributes="Bold"></Label>
<Label Text="{Binding Main.Cnpj}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Cargo: " FontAttributes="Bold"></Label>
<Label Text="{Binding Cargo}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="Inicio" FontAttributes="Bold"></Label>
<Label Text="{Binding DataInicio}"></Label>
<Label Text="Término" FontAttributes="Bold"></Label>
<Label Text="{Binding DataFim}"></Label>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
public class ModelosPPP
{
public Empresa Main { get; set; }
public string DataInicio { get; set; }
public string DataFim { get; set; }
public string Cargo { get; set; }
public string Status { get; set; }
}
public class Empresa
{
public string Nome { get; set; }
public string Cnpj { get; set; }
}