我使用Xamarin表單來獲取「訂單」,並從我在ROR中構建的api的表視圖中列出它們。Xamarin表單綁定屬性不顯示
一個例子返回如下所示:
[
{
"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以上,對於調試輸出如下所示:
[Order:ID = 6,Description =測試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>
我必須使用相同的屬性一些默認的訂單,當我添加兩個到列表的末尾,他們出現!
我缺少什麼?
UPDATE -----------------------
這裏是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警告? – user2657943
你如何設置你的'BindingContext'? –
沒有XAML警告。我附上了項目中所有當前的警告。 –