【发布时间】:2017-06-13 04:18:10
【问题描述】:
我正在使用 Xamarin Forms 获取“订单”,并通过我在 ROR 中构建的 api 在表格视图中列出它们。
示例返回如下所示:
[
{
"id": 6,
"invoice": "Testing API",
"description": "Testing the API readouts",
"weight": 100,
"length": 100,
"width": 100,
"height": 100,
"account_id": 1,
"driver_id": null,
"state": "needs_driver",
"account_quote": 1041,
"driver_quote": 781,
"created_at": "2017-06-10T02:03:57.458Z",
"updated_at": "2017-06-10T02:04:05.535Z",
"driver_rating": 5,
"account_rating": 5,
"guid": "a3bb60e5-8744-44fe-9554-a1e26867d411"
}
]
下面是解析返回的代码:
using (var client = new HttpClient())
{
var responseText = await client.GetStringAsync(url);
List<Order> data = JsonConvert.DeserializeObject<List<Order>>(responseText);
orders = new ObservableCollection<Order>(data);
Debug.WriteLine(orders.Count);
foreach (Order order in orders)
{
Debug.WriteLine(order.ToString());
}
}
对于上面的示例 json,Debug 的输出如下所示:
1
[订单:ID=6,描述=测试 API 读数,OrderStatus=预览,PickupContact=,DropoffContact=,PickupAddress=,DropoffAddress=,PickupTime=1/1/0001 12:00:00 AM,DropoffTime=1 /1/0001 12:00:00 AM,PickupTimeFormatted=1/1/0001 12:00 AM,DropoffTimeFormatted=1/1/0001 12:00 AM]
但是,视图看起来像一张空白表格。 :(
这是列表页的 XAML:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Divco.MyOrdersPage" Title="My Schedule">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="OrderView" ItemsSource="{Binding Orders}" ItemTapped="OnOrderSelected">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding description}" Detail="{Binding PickupTimeFormatted}" DetailColor="Fuschia" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
我有一些使用相同属性的默认订单,当我将两个添加到列表末尾时,它们会出现!
我错过了什么?
更新-----------
这里是 MyOrdersPage.cs 的开头
public partial class MyOrdersPage : ContentPage
{
ObservableCollection<Order> orders = new ObservableCollection<Order>();
public MyOrdersPage(string response)
{
InitializeComponent();
//Debug.WriteLine(response);
UpdateOrders();
OrderView.ItemsSource = orders;
OrderView.IsPullToRefreshEnabled = true;
// sample orders for testing, comment when able to pull information from heroku
//orders.Add(Order.DefaultOrder);
//orders.Add(Order.DefaultOrder);
}
}
注意:当我取消注释“orders.Add(Order.DefaultOrder);”时,我用于测试的两个默认订单出现在列表中。
这是取消注释最后两个“orders.Add(Order.DefaultOrder);”的屏幕截图
【问题讨论】:
-
尝试将
Text="{Binding description}"更改为Text="{Binding Description}"另外,当您运行应用程序时,您是否在调试输出中看到xaml/binding 警告? -
如何设置
BindingContext? -
无 XAML 警告。我已经附上了项目中所有当前的警告。
标签: c# json xamarin.ios xamarin.forms json.net