이것은 내 C# 코드입니다. 이제 콘솔에 데이터가 표시됩니다.Xaml, Windows Phone에 데이터를 바인딩하는 방법은 무엇입니까?
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
};
}
데이터를 텍스트 블록으로 표시하려면 어떻게해야합니까?
이것은 현재 xaml 코드입니다. 확실히 작동하지 않습니다. 나는 정말로 모른다.
<ListBox ItemsSource="{Binding Places}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding Path=id}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
당신에게 대단히 감사합니다! –
작동하지 않습니다. 코드를 편집했습니다. 어떠한 제안? –
당신은 ViewModel 클래스를 만들까요? 가져온 데이터로 속성을 채웠습니까?당신은 ViewModel을 DataContext에 할당합니까? – Abbas