如何在Xamarin Forms的CollectionView中显示嵌套数组
解决Xamarin Forms中CollectionView展示嵌套Estado数组的问题
你的代码核心思路没问题,用BindableLayout嵌套展示数组是正确方案,以下是具体优化和排查步骤:
1. 确保JSON反序列化正确
MongoDB的特殊类型(ObjectId、ISODate)可能导致反序列化时Status数组未被正确填充,建议补充模型字段并配置序列化规则:
// 完善Estado模型,添加缺失的createdAt字段确保反序列化 public class Estado { [JsonProperty("createdAt")] public DateTime FechaEstado { get; set; } [JsonProperty("value")] public string EstadoPago { get; set; } [JsonProperty("name")] public string EstadoTransaccion { get; set; } }
如果使用Newtonsoft.Json,可添加MongoDB类型转换器来处理ObjectId和ISODate,避免反序列化失败。
2. 优化XAML布局展示
你的XAML已具备嵌套展示的基础,可优化样式提升可读性:
<CollectionView ItemsSource="{Binding ItemsSubasta}" SelectionMode="Single"> <CollectionView.ItemTemplate> <DataTemplate> <Frame BackgroundColor="#eee" BorderColor="#ddd" Padding="20"> <StackLayout x:DataType="model:TicketsApi"> <Label Text="{Binding Nombresolicitante, StringFormat='Nombres: {0}'}" LineBreakMode="NoWrap" FontSize="14" /> <Label Text="{Binding Apellidosolicitante, StringFormat='Apellidos: {0}'}" LineBreakMode="NoWrap" FontSize="14" /> <Label Text="{Binding CitaSolicitante, StringFormat='Fecha de consulta: {0:yyyy-MM-dd HH:mm}'}" LineBreakMode="NoWrap" FontSize="16" /> <Label Text="{Binding TiempoSolicitante, StringFormat='Tiempo: {0} minutos'}" LineBreakMode="NoWrap" FontSize="13" /> <Label Text="{Binding ValorSolicitante, StringFormat='Valor Consulta: {0:F2} dólares (incluye IVA)'}" LineBreakMode="NoWrap" FontSize="13" /> <!-- 嵌套展示Estado数组,添加标题区分 --> <Label Text="Estado de transacción:" FontSize="14" FontAttributes="Bold" Margin="0,10,0,5"/> <StackLayout BindableLayout.ItemsSource="{Binding Status}"> <BindableLayout.ItemTemplate> <DataTemplate x:DataType="model:Estado"> <StackLayout Orientation="Horizontal" Margin="0,2,0,2"> <Label Text="{Binding EstadoTransaccion, StringFormat='Tipo: {0}'}" FontSize="13"/> <Label Text="{Binding EstadoPago, StringFormat=' | Estado: {0}'}" FontSize="13" Margin="5,0,0,0"/> </StackLayout> </DataTemplate> </BindableLayout.ItemTemplate> </StackLayout> </StackLayout> </Frame> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView>
3. 排查绑定异常
如果数组仍不显示,按以下步骤排查:
- 检查
ItemsSubasta中每个TicketsApi对象的Status属性是否为null或空数组 - 确认页面根元素已正确引用模型命名空间:
xmlns:model="clr-namespace:App1.Models"
- 开启绑定调试,在
App.xaml.cs中添加:
public App() { InitializeComponent(); BindingBase.EnableCollectionSynchronization(); }
原始问题相关代码
获取的JSON响应
{ "_id" : ObjectId("634b6be99237b35a9aa80dd5"), "updatedAt" : ISODate("2022-10-16T02:27:44.766+0000"), "createdAt" : ISODate("2022-10-16T02:26:49.276+0000"), "firstName" : "Romulo", "lastName" : "Calderon", "email" : "romuloacd@gmail.com", "phone" : "0962908538", "description" : "Hoy u", "user" : ObjectId("5aee09ef88b9ac4289109574"), "appoinmentAt" : ISODate("2022-10-17T14:00:00.000+0000"), "status" : [ { "createdAt" : ISODate("2022-08-22T20:15:48.672+0000"), "value" : "DEUDA", "name" : "WITHOUT_PAYING" }, { "createdAt" : ISODate("2022-08-22T20:15:48.672+0000"), "value" : "PAGADO", "name" : "PAID_OUT" } ], "total" : 45.36, "iva" : NumberInt(12), "value" : 40.5, "time" : NumberInt(30), "__v" : NumberInt(0) }
对应的模型代码
namespace App1.Models { public class TicketsApi { [JsonProperty("_id")] public string Idtickets { get; set; } [JsonProperty("user")] public string Idusuario { get; set; } [JsonProperty("firstName")] public string Nombresolicitante { get; set; } [JsonProperty("lastName")] public string Apellidosolicitante { get; set; } [JsonProperty("email")] public string CorreoSolicitante { get; set; } [JsonProperty("description")] public string Motivoconsulta { get; set; } [JsonProperty("phone")] public string CelularSolicitante { get; set; } [JsonProperty("value")] public string ValorSolicitante { get; set; } [JsonProperty("time")] public string TiempoSolicitante { get; set; } [JsonProperty("status")] public Estado[] Status { get; set; } [JsonProperty("appoinmentAt")] public DateTime CitaSolicitante { get; set; } [JsonProperty("createdAt")] public DateTime FechaCreacion { get; set; } } public class Estado { [JsonProperty("value")] public string EstadoPago { get; set; } [JsonProperty("name")] public string EstadoTransaccion { get; set; } } }
已实现的XAML代码
<CollectionView ItemsSource="{Binding ItemsSubasta}" SelectionMode="Single"> <CollectionView.ItemTemplate> <DataTemplate> <Frame BackgroundColor="#eee" BorderColor="#ddd" Padding="20"> <StackLayout x:DataType="model:TicketsApi"> <Label Text="{Binding Nombresolicitante, StringFormat='Nombres: {0:N}'}" LineBreakMode="NoWrap" FontSize="14" /> <Label Text="{Binding Apellidosolicitante, StringFormat='Apellidos: {0:N}'}" LineBreakMode="NoWrap" FontSize="14" /> <Label Text="{Binding CitaSolicitante, StringFormat='Fecha de consulta: {0}'}" LineBreakMode="NoWrap" FontSize="16" /> <Label Text="{Binding TiempoSolicitante, StringFormat='Tiempo: {0:N} minutos'}" LineBreakMode="NoWrap" FontSize="13" /> <Label Text="{Binding ValorSolicitante, StringFormat='Valor Consulta: {0:N} dólares (incluye IVA)'}" LineBreakMode="NoWrap" FontSize="13" /> <StackLayout BindableLayout.ItemsSource="{Binding Status}"> <BindableLayout.ItemTemplate> <DataTemplate x:DataType="model:Estado"> <Label Text="{Binding EstadoTransaccion }" /> </DataTemplate> </BindableLayout.ItemTemplate> </StackLayout> </StackLayout> </Frame> </DataTemplate> </CollectionView.ItemTemplate> </CollectionView>
内容的提问来源于stack exchange,提问作者Oswaldo Roosemberg
相关产品推荐
相关产品推荐

