【问题标题】:Xamarin Forms Binding properties not appearingXamarin Forms 绑定属性未出现
【发布时间】: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


【解决方案1】:

抱歉英语不好

所以你不使用 ViewModel。好吧。我们开始:

似乎您的 UpdateOrders() 方法是异步的。这使得 SO 使用线程来处理这些指令。这种情况下的一些错误不会显示,也不会导致应用程序崩溃。您的线程 (UpdateOrders) 正在对您的应用程序主线程进行更改,因此您需要在 BeginInvokeOnMainThread 方法块内进行更改。

试试这个:

在 UpdateOLrders 方法中,在 using 块中,而不是 orders = new ObservableCollection&lt;Order&gt;(data);

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

这可能有效,但远非做这种事情的最佳方式。 我建议你阅读thisthis 的文章。希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 2017-12-21
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多