在TextBlocks中使用其他Web服务

时间:2015-12-11 11:08:40

标签: c# win-universal-app windows-10-universal

我想使用一个休息网络服务,在我的网格中的stackPanels中显示每个菜肴的名称,如下所示:

enter image description here

但是使用我的代码,我只得到最后一个元素,如何在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;
            }}

这次我得不到任何结果:(

1 个答案:

答案 0 :(得分:1)

您可以使用以下代码:

<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,以便获得更清晰的代码。