【发布时间】:2019-12-11 00:09:34
【问题描述】:
我有一个无法解决的问题,希望你能帮助我。
在我的 Xamarin.Forms 应用程序中,我正在下载我想在 ListView 中显示的 JSON 数据。
JSON 嵌套数据如下所示:
{
"devices": [
{
"id": 2,
"name": "device1",
"owner": {
"owner_id": 10,
"owner_surname": "XYZ"
}
},
{
"id": 3,
"name": "device3",
"owner": {
"owner_id": 12,
"owner_surname": "XVB"
}
}
]
}
我对这些数据有这样的类:
public class Items
{
public List<Device> devices { get; set; }
}
public class Device
{
public int id { get; set; }
public string name { get; set; }
public Owner owner { get; set; }
}
public class Owner
{
public int owner_id { get; set; }
public string owner_surname { get; set; }
}
这是我的 MainPage.xaml.cs 文件:
var client = new WebClient();
client2.Headers.Add("Accept", "application/json");
var content = client.DownloadString(Url);
var tr = JsonConvert.DeserializeObject<List<Items>>(content);
ObservableCollection<Items> trends = new ObservableCollection<Items>(tr);
myList.ItemsSource = trends;
这是我在 XAML 中的 ListView:
<ListView x:Name="myList" HasUnevenRows="true" ItemsSource="{Binding devices}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding name}" TextColor="Black" FontAttributes="Bold"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
此代码不显示任何内容(白屏)。 我想显示第一级嵌套的“名称”和第二级嵌套的“所有者姓氏”。 应用基于:https://www.codementor.io/lutaayahuzaifahidris/xamarin-forms-simple-list-of-items-with-offline-capability-cross-platform-app-btyq0bihv 对stackoverflow的其他响应不起作用:(
【问题讨论】:
标签: c# .net xaml listview xamarin.forms