这是我第一次创建windows8应用程序,原因很简单,因为我必须为学校项目创建应用程序。我对xaml中的数据绑定等并不陌生,但在创建W8应用程序时是否有所不同,因为它的工作方式与我平时不同。
XAML代码:(我的数据模板在)
<ItemsControl ItemTemplate="{StaticResource test}" DataContext="{Binding ListLineup}">
<DataTemplate x:Key="test">
<StackPanel>
<TextBlock Text="{Binding Date}"></TextBlock>
</StackPanel>
</DataTemplate>public class LineUp
{
public string Id { get; set; }
public string Date { get; set; }
public string From { get; set; }
public string Until { get; set; }
public LineUp(string id, string date, string from, string until)
{
this.Id = id;
this.Date = date;
this.From = from;
this.Until = until;
}
public static async Task<List<LineUp>> GetLineUp()
{
List<LineUp> lineup = new List<LineUp>();
using (HttpClient client = new HttpClient())
{
string url = @"http://localhost:28603/api/LineUp";
Uri uri = new Uri(url);
using (HttpResponseMessage response = await client.GetAsync(uri))
{
if (response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync();
content = "{'lineups':" + content + "}";
ListLineUp CollectionOfLineUps = await JsonConvert.DeserializeObjectAsync<ListLineUp>(content);
foreach (LineUp newLineup in CollectionOfLineUps.lineups)
{
lineup.Add(newLineup);
}
}
else
{
Debug.WriteLine("Exception when getting the LineUps. API is down ");
}
}
}
return lineup;
}
}
public class ListLineUp
{
public List<LineUp> lineups { get; set; }
} public async void GetAllNeededLists()
{
ListLineup = await LineUp.GetLineUp();
foreach (var lu in ListLineup)
{
Debug.WriteLine(lu.Date);
}
}运行时,我在调试窗口中获取所有数据和我的日期。当我运行应用程序时,有一个文本块,但其中没有内容。
发布于 2014-01-04 05:06:00
要在ItemsControl中显示项目,需要使用IEnumerable对象设置ItemsSource属性。
<ListView ItemsSource="{Binding lineups}"/>并将您的ListLineUp对象设置为ListView上的DataContext。
若要设置DataTemplate,请使用ItemTemplate属性。
<ListView.ItemTemplate>
<DataTemplate>...</DataTemplate>
</ListView.ItemTemplate>发布于 2014-01-04 05:07:22
您需要设置ItemsControl的ItemsSource,而不是DataContext:
<ItemsControl ItemTemplate="{StaticResource test}" ItemsSource="{Binding ListLineup}">https://stackoverflow.com/questions/20912609
复制相似问题