2012-08-07 76 views
0

大家好,我有一個小問題,我希望有人能幫助我 我有一個JSON的網址,讓我這樣的數據:WP7 listpicker,從JSON結合

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

然後我加入的ItemSource = {結合NOM}在listpicker在XAML

任何幫助我會很感激,併爲我的英語感到抱歉

+0

什麼確切的問題用這個?你有沒有得到錯誤或沒有得到預期的結果? – 2012-08-07 09:02:20

回答

0

它的好,我找到了答案,我已經用第二種方法

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

我相信你問的是綁定沒有生效。也就是說,你看不到列表中的數據。

如果是這樣,你是否將列表選擇器的DataContext設置爲List?另外,當你收到更多的JSON數據時,你會希望列表選擇器的內容發生變化。所以,如果是這種情況,我會建議您使用ObservableCollection而不是List。