在 TextBlocks 中使用休息网络服务
consume rest web service in TextBlocks
我想使用一个休息 web 服务,它在我的网格中的 stackPanels 中显示每个菜系的名称,如下所示:
但是用我的代码,我只得到最后一个元素,我怎样才能在 stackPanel 中发送厨房的每个名称,这是我的代码:
<Grid x:Name="Grid1" >
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=nom}" x:Name="nomCuisine"/>
</StackPanel>
</StackPanel>
</Grid>
这是我的职能:
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;
}
}
这是我的json数据:
success: 1,
message: "cuisine found!",
cuisines: [
{
id: "1",
nom: "Cuisine 1"
},
{
id: "2",
nom: "Cuisine 2"
}
]
感谢帮助
更新:
我将代码设置为:
<ListView ItemsSource="{Binding items}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="" x:Name="nomCuisine"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是函数:
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;
}}
这次我一无所获:(
您可以使用以下代码:
<ListView x:Name="cuisineListview">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding nom}" x:Name="nomCuisine"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
并且:
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;
}
但我建议您学习如何使用 MVVM,这样您就可以获得更简洁的代码。
我想使用一个休息 web 服务,它在我的网格中的 stackPanels 中显示每个菜系的名称,如下所示:
但是用我的代码,我只得到最后一个元素,我怎样才能在 stackPanel 中发送厨房的每个名称,这是我的代码:
<Grid x:Name="Grid1" >
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Path=nom}" x:Name="nomCuisine"/>
</StackPanel>
</StackPanel>
</Grid>
这是我的职能:
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;
}
}
这是我的json数据:
success: 1,
message: "cuisine found!",
cuisines: [
{
id: "1",
nom: "Cuisine 1"
},
{
id: "2",
nom: "Cuisine 2"
}
]
感谢帮助
更新:
我将代码设置为:
<ListView ItemsSource="{Binding items}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="" x:Name="nomCuisine"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是函数:
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;
}}
这次我一无所获:(
您可以使用以下代码:
<ListView x:Name="cuisineListview">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding nom}" x:Name="nomCuisine"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
并且:
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;
}
但我建议您学习如何使用 MVVM,这样您就可以获得更简洁的代码。