简体   繁体   中英

wp7 listpicker ,binding from json

Hi everybody i have a little problem and i hope that somebody could help me I have a json url that gives me the data like this :

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

My class :

public class ListApplication
    {

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

    }

I have a listpicker :

I want to bind just the element “nom” in the listpicker, I've tried this methods but nothing works: The first method:

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;
                //
            }

The second method:

 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://........"));
        }

And then i added Itemsource= {Binding nom} in the listpicker in XAML

Any help I'll be so appreciative ,and sorry for my English

I believe you are asking that the binding is not taking effect. That is, you cannot see the data in your list.

If that is the case, are you setting the DataContext of the list picker to the List? Also, it seems that you would expect the content of the list picker to change when you receive more JSON data. So, if that is the case i would advise you to use an ObservableCollection instead of a List.

It's ok i found the answer ,i've used this with the second method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM