2017-06-12 94 views
0

我使用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);」

enter image description here

這裏是我的項目的警告,如果這有助於... enter image description here

+0

嘗試將'Text =「{Binding description}」'更改爲'Text =「{Binding Description}」 此外,當您運行應用程序時,是否在調試輸出中看到xaml/binding警告? – user2657943

+0

你如何設置你的'BindingContext'? –

+0

沒有XAML警告。我附上了項目中所有當前的警告。 –

回答

1

對不起糟糕的英語

所以你不使用視圖模型。好吧。這裏我們去:

好像你的UpdateOrders()方法是異步的。這使得SO使用一個線程來處理這些指令。這種情況下的一些錯誤不會顯示,並且不會使應用程序崩潰。您的線程(UpdateOrders)正在對您的應用程序主線程進行更改,因此您需要在BeginInvokeOnMainThread方法塊內執行更改。在使用塊

裏面你UpdateOLrders方法,而不是orders = new ObservableCollection<Order>(data);

foreach(var item in data) 
{  
    Device.BeginInvokeOnMainThread(() => orders.Add(item)); 
} 

這可能工作,但它做這種最好的方式是遠:

試試這個的東西。 我建議你閱讀thisthis文章。我希望這是有幫助的。