在我的 ComboBox 中获取单个属性名称

get a single attribut name in my ComboBox

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

 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"
}
]

我怎样才能更正我的代码,使我的组合框中的每个名称都没有双倍的,我认为我的代码是正确的,但我的组合框中的值总是双倍的 >_<

这是结果 感谢帮助

我认为您的 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;
                 }