wp7 listpicker,来自json的绑定

时间:2012-08-07 08:51:17

标签: c# json windows-phone-7

大家好我有一点问题,我希望有人可以帮助我 我有一个json url,它给我这样的数据:

[
  {
    "nom": "fofo",
    "appGuid": "79fa058b-395a-438d-b66f-d751faea82e0"
  },
  {
    "nom": "fifi",
    "appGuid": "8b6bfcdb-d286-46e2-889e-0168a782323f"
  },
  {
    "nom": "toto",
    "appGuid": "65DE39E7-0130-4836-BBD3-7051574018B6"
  },
  {
    "nom": "titi",
    "appGuid": "66DE39E7-0130-4836-BBD3-7051574018B6"
  }
]

我的课程:

public class ListApplication
    {

            public string nom { get; set; }
            public string appGuid { get; set; }

    }

我有一个listpicker:             

我想在listpicker中绑定元素“nom”,我尝试过这种方法,但没有任何效果: 第一种方法:

WebClient visio = new WebClient();
            visio.DownloadStringCompleted += new DownloadStringCompletedEventHandler(vision_DownloadStringCompleted);
            visio.DownloadStringAsync(new Uri("https://......... "));

void vision_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            JArray jArray = JArray.Parse(e.Result);
            List<ListApplication> apps = new List<ListApplication>();

            for (int i = 0; i < jArray.Count; i++)
            {
                JObject app = (JObject)jArray[i];
                apps.Add(new ListApplication { nom = (string)app["nom"], appGuid = (string)app["appGuid"] });
                this.Application.ItemsSource = apps;
                //
            }

第二种方法:

 public Appli()
        {
            InitializeComponent();
            this.Type_info.ItemsSource = Action;
            this.Periode.ItemsSource = Per;
            var w = new WebClient();
            Observable.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted").Subscribe(r =>
              {
                  var deserialized =
                    JsonConvert.DeserializeObject<List<ListApplication>>(r.EventArgs.Result);
                  Application.ItemsSource = deserialized;
              });
            w.DownloadStringAsync(
              new Uri("https://........"));
        }

然后我在XAML的listpicker中添加了Itemsource = {Binding nom}

任何帮助我都会非常感激,对不起我的英语

2 个答案:

答案 0 :(得分:0)

我相信你要求绑定没有生效。也就是说,您无法在列表中看到数据。

如果是这种情况,您是否将列表选择器的DataContext设置为List?此外,当您收到更多JSON数据时,似乎您希望列表选择器的内容发生更改。所以,如果是这种情况,我建议你使用ObservableCollection而不是List。

答案 1 :(得分:0)

没关系,我找到了答案,我用第二种方法

<toolkit:ListPicker x:Name="listPicker" Header="Application" >
                <toolkit:ListPicker.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding nom}" />
                        </StackPanel>
                    </DataTemplate>
                </toolkit:ListPicker.ItemTemplate>
            </toolkit:ListPicker>