Reputation: 6996
I'm a windows phone 8 novice, struggling at the moment with xaml data-binding with arrays.
XAML:
<phone:LongListSelector x:Name="LocationsData" Margin="0,0,-12,0" ItemsSource="{Binding}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17">
<TextBlock Text="{Binding name}" TextWrapping="NoWrap" Margin="12,0,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding vicinity}" TextWrapping="NoWrap" Margin="12,-6,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
.CS
LocationsData.DataContext = _array_of_locations;
The _array_of_locations
has 4 items,
each of them is a valid JSON,
{
"geometry": {
"location": {
"lat": 12.923101,
"lng": 77.586057
}
},
"icon": "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id": "9a769bc2faaa79fe0ffbd85c2c28446940153331",
"name": "Domino's Pizza",
"opening_hours": {
"open_now": false
},
"price_level": 1,
"rating": 2.9,
"reference": "CnRsAAAAsI0LvfwZ_RC8PEDsJS3TfKkRkTn7d_2_-vw8tu_SYBYCJk2CmKt6RyRJtO5mG0Weq-R0jSsmyQOWHjU45itlrH1cN89EqgIA9Vtmvcih1xi6ZwpNewqZ8mNCQWWLDJvcT3AQLHGnFcn4E9a30Gvs9xIQeKGVsrSOKLDx4vYCjixIKhoUOGeosCJIVFDmE3-3qIPcIM7PSCs",
"types": [
"restaurant",
"food",
"establishment"
],
"vicinity": "11th Main Road, Jayanagar, Bangalore"
}
It's not working, I'm getting lost with all the documentation as I don't come from a c# background. So how do I get this working?
Upvotes: 0
Views: 622
Reputation: 64959
The problem isn't the arrays here, it's the fact that the source of a data binding (i.e. the name
and vicinity
in {Binding name}
and {Binding vicinity}
) must be a property. The objects in your array don't have properties with names name
and vicinity
, so nothing gets shown. In fact, you might even see binding errors in the Output window in Visual Studio, mentioning that it couldn't find name
and vicinity
properties.
I suspect you have got four items in your LongListSelector
, but because none of them have any text in them, they have zero size and you don't see them. Try adding a TextBlock
with some static text (e.g. <TextBlock Text="X"/>
) to your StackPanel
to see if four X
s appear.
If you just need to show the JSON read-only, then it should be possible to deserialize the JSON to anonymous types. An anonymous type will then be created for each JSON object, with one (.NET) property for each property in the JSON object. See for example this question. With any luck that should work, however, I haven't tested it.
Upvotes: 2