C# 将数据绑定到Windows 8应用程序的ListView

C# 将数据绑定到Windows 8应用程序的ListView,c#,.net,xaml,listview,windows-store-apps,C#,.net,Xaml,Listview,Windows Store Apps,我希望有人能帮我解决这个问题,因为它让我发疯。我正在为一个工作项目编写一个原型应用程序,这也是我的第一个Windows8应用程序。我所要做的就是获取一个ListView控件来显示集合中的一些信息,我已经包含了下面的所有代码。我似乎无法让它与我实际想要显示的集合一起工作,但是如果我创建了一个测试类和集合,我可以很好地显示它,代码也包括在内。我完全被这一点弄糊涂了,所以这肯定是我错过了什么,可能是看不见树木 任何指点都将不胜感激 测试代码(工作正常): 代码隐藏: public class Pers

我希望有人能帮我解决这个问题,因为它让我发疯。我正在为一个工作项目编写一个原型应用程序,这也是我的第一个Windows8应用程序。我所要做的就是获取一个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();
    }
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();
    }
公共类人物
{
公共字符串名{get;set;}
公共字符串姓氏{get;set;}
}
私有列表GetPeople()
{
var people=新列表();
添加(newperson(){Firstname=“John”,姓氏=“Smith”});
添加(newperson(){Firstname=“Tom”,姓氏=“Jones”});
people.Add(newperson(){Firstname=“Barry”,姓氏=“Thomas”});
还人,;
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
OrdersListView.DataContext=this.GetPeople();
}
XAML:


实际代码(不显示任何内容):

代码隐藏:

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();
    }
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();
    }
private async Task LoadData()
{
常量字符串serviceUrl=”http://localhost:54879/";
var client=新的HttpClient
{
BaseAddress=新Uri(serviceUrl)
};
HttpResponseMessage response=wait client.GetAsync(“api/orders”);
var orders=wait response.Content.ReadAsAsync();
返回订单。ToList();
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
OrdersListView.DataContext=this.LoadData();
}
XAML:


您必须将
wait
置于
LoadData()
上,以将
DataContext
设置为
任务的结果

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

啊!当然了!我说这是显而易见的。谢谢你的吹毛求疵。