为Windows 8应用程序绑定数据到ListView

本文关键字:数据 ListView 绑定 应用程序 Windows | 更新日期: 2023-09-27 18:09:12

我希望有人能帮我解决这个问题,因为它把我逼疯了。我正在为工作中的一个项目编写一个原型应用程序,这也是我的第一个Windows 8应用程序。我所要做的就是获得一个ListView控件来显示一个集合中的一些信息,我已经包含了下面的所有代码。我似乎无法让它与我实际想要显示的集合一起工作,但如果我创建一个测试类和集合,我可以显示那个很好,代码也包括在内。我完全被这个迷惑了,所以一定是我错过了什么明显的东西,可能是只见树木不见树木。

请指教。

测试代码(工作正常):

背后的代码:

public class Person
{
    public string Firstname { get; set; }
    public string Surname { get; set; }
}
private List<Person> GetPeople()
    {
        var people = new List<Person>();
        people.Add(new Person() { Firstname = "John", Surname = "Smith" });
        people.Add(new Person() { Firstname = "Tom", Surname = "Jones" });
        people.Add(new Person() { Firstname = "Barry", Surname = "Thomas" });
        return people;
    }
protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        OrdersListView.DataContext = this.GetPeople();
    }
XAML:

    <ListView x:Name="OrdersListView" HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding}" Width="auto" Height="auto">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Text="Name: " Margin="2" />
                    <TextBlock Text="{Binding Firstname}" Margin="2" />
                    <TextBlock Text="{Binding Surname}" Margin="2" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

实际代码(无显示):

背后的代码:

private async Task<List<Order>> LoadData()
    {
        const string serviceUrl = "http://localhost:54879/";
        var client = new HttpClient
                     {
                         BaseAddress = new Uri(serviceUrl)
                     };
        HttpResponseMessage response = await client.GetAsync("api/orders");
        var orders = await response.Content.ReadAsAsync<Order[]>();
        return orders.ToList();
    }
protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        OrdersListView.DataContext = this.LoadData();
    }
XAML:

<ListView x:Name="OrdersListView" HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding}" Width="auto" Height="auto">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="2">
                    <TextBlock Text="Order Ref: " Margin="2" />
                    <TextBlock Text="{Binding OrderReference}" Margin="2" />
                    <TextBlock Text="Name: " Margin="2" />
                    <TextBlock Text="{Binding Firstname}" Margin="2" />
                    <TextBlock Text="{Binding Surname}" Margin="2" />
                    <TextBlock Text="Address: " Margin="2" />
                    <TextBlock Text="{Binding Address1}" Margin="2" />
                    <TextBlock Text="{Binding Address2}" Margin="2" />
                    <TextBlock Text="{Binding TownCity}" Margin="2" />
                    <TextBlock Text="{Binding Country}" Margin="2" />
                    <TextBlock Text="{Binding Postcode}" Margin="2" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

为Windows 8应用程序绑定数据到ListView

您必须将await放在LoadData()上以设置DataContext作为Task的结果。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    OrdersListView.DataContext = await this.LoadData();
}