2017-06-12 97 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文章。我希望这是有帮助的。