这是我的 C# 代码,现在它在控制台中显示数据。
class PlaceViewModel
{
public List<Vantaa.IntroPage.Place> Places;
}
public class Place
{
public string id { get; set; }
public string title { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string www { get; set; }
}
public class RootObject
{
public List<Place> Places { get; set; }
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri("http://mobiilivantaa.lightscreenmedia.com/api/place"));
}
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
foreach (var book in rootObject.Places)
{
System.Diagnostics.Debug.WriteLine(book.id);
}
this.DataContext = new PlaceViewModel
{
Places = rootObject.Places
};
}
为了在 textblock 中显示数据,我应该如何处理 xaml 文件?
这是我当前的 xaml 代码。它肯定行不通。我真的不知道。
<ListBox ItemsSource="{Binding Places}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=id}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>