我正在尝试为我的以下JSON数据实现listview分组。
JSON示例:
{ "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 ListcbrainBibleBooksHB { get; set; } } public class CbrainBibleBooksHB : ObservableCollection { public string book { get; set; } public List cbrainBibleTOList { get; set; } } public class CbrainBibleTOList { public string chapter { get; set; } public string pageUrl { get; set; } public string 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(response.ToString()); } AllItems = new ObservableCollection (bibleTestament.cbrainBibleBooksHB);
XAML
但是,在运行项目时,UI上没有任何数据显示。Binding: 'book' property not found on 'System.Object[]', target property: 'Xamarin.Forms.Label.Text'
在输出框中获取消息。以xamarin形式实现对列表视图的分组非常困难。谁能帮我做到这一点?我在这里上传了一个示例项目。
您可以使用最新的Xamarin.Forms版本== 3.5的BindableLayout来代替使用成组的Listview,而无需花费很多精力。
更新您的模型类
public class CbrainBibleBooksHB { public string book { get; set; } public ListcbrainBibleTOList { get; set; } }
XAML:
注意:此处ParentContentPage是父内容页面的x:Name,用于为命令提供参考。
ViewModel:
class BibleTestamentViewModel : INotifyPropertyChanged { public ICommand TapCommand { get; private set; } public BibleTestamentViewModel() { TapCommand = new Command(ChapterClickedClicked); } private void ChapterClickedClicked(object sender) { //check value inside sender } }
输出: