I want to consume a rest web service that shows the name of each cuisine in stackPanels in my Grid like this:
but with my code,I get only the last element,How can I send each name of kitchen in a stackPanel ,this is my code:
<Grid x:Name="Grid1" >
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=nom}" x:Name="nomCuisine"/>
</StackPanel>
</StackPanel>
</Grid>
and this is my function:
private async void GetListeCuisines()
{
UriString2 = "URL/cuisines.php";
var http = new HttpClient();
http.MaxResponseContentBufferSize = Int32.MaxValue;
var response = await http.GetStringAsync(UriString2);
var rootObject = JsonConvert.DeserializeObject<Barberry.Models.RootObject>(response);
for(int i=0; i < 2;i++)
{
nomCuisine.Text = rootObject.cuisines[i].nom;
}
}
this is my json data:
success: 1,
message: "cuisine found!",
cuisines: [
{
id: "1",
nom: "Cuisine 1"
},
{
id: "2",
nom: "Cuisine 2"
}
]
thanks for help
Update:
I set my code to this:
<ListView ItemsSource="{Binding items}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="" x:Name="nomCuisine"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and this is the function:
private async void GetListeCuisines()
{
UriString2 = "myURL/cuisines.php";
var http = new HttpClient();
http.MaxResponseContentBufferSize = Int32.MaxValue;
var response = await http.GetStringAsync(UriString2);
var rootObject = JsonConvert.DeserializeObject<Barberry.Models.RootObject>(response);
for(int i=0; i < 2;i++)
{
string items= rootObject.cuisines[i].nom;
}}
this time I get nothing as a result :(
You can use the following code:
<ListView x:Name="cuisineListview">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding nom}" x:Name="nomCuisine"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
And:
private async void GetListeCuisines()
{
UriString2 = "myURL/cuisines.php";
var http = new HttpClient();
http.MaxResponseContentBufferSize = Int32.MaxValue;
var response = await http.GetStringAsync(UriString2);
cuisineListview.ItemsSource = JsonConvert.DeserializeObject<Barberry.Models.RootObject>(response).cuisines;
}
But I suggest you to learn how to use MVVM so you can have cleaner code.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments