在我的ComboBox中获取一个attribut名称

时间:2016-01-11 02:55:43

标签: c# combobox win-universal-app

我在设置"名称"时遇到问题从Web服务文本到我的comboBox,实际上我在我的comboBox中得到了每个名字的两倍,这是我的代码:

 private async void getCategories()
        {
                Uri = "myWebService";
                http = new HttpClient();
                http.MaxResponseContentBufferSize = Int32.MaxValue;
                var response = await http.GetStringAsync(Uri);
                var rootObject1 = JsonConvert.DeserializeObject<NvBarberry.Models.RootObject>(response);
                List<Categories> v = rootObject1.categories.ToList();

                for (int i = 0; i < v.Count; i++) { 
                    this.categoryCombo.Items.Add(v[i].name);
                       }
                this.categoryCombo.SelectedIndex = 0;
                     }

这是我的网络服务:

success: 1,
message: "categorie trouve!",
total: 2,
categories: [
{
id: "1",
name: "resto",
descr: "restaurant pizza"
},
{
id: "2",
name: "test",
descr: "test"
}
]

如何更正我的代码,使我的comboBox中没有每个名称的两倍,我认为我的代码是正确的,但我的组合框中总是得到一个双倍值&gt; _&lt;

这是结果 enter image description here 谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

我认为你的getCategories被调用了2次。只需设置断点,看它是否达到2次。但是为了确保你的组合框包含一个项目,只需清除它,然后在这里添加项目就是代码。

   private async void getCategories()
    {
            Uri = "myWebService";
            http = new HttpClient();
            http.MaxResponseContentBufferSize = Int32.MaxValue;
            var response = await http.GetStringAsync(Uri);
            var rootObject1 = JsonConvert.DeserializeObject<NvBarberry.Models.RootObject>(response);
            List<Categories> v = rootObject1.categories.ToList();

            this.categoryCombo.Items.Clear();
            for (int i = 0; i < v.Count; i++) { 
                this.categoryCombo.Items.Add(v[i].name);
                   }
            this.categoryCombo.SelectedIndex = 0;
                 }