2013-12-14 42 views
0

我目前正在爲windows phone 8開發一個應用程序,它從互聯網抓取JSON數據,將其解析爲集合,將此集合綁定到列表框並顯示它。這工作得很好,我不喜歡這樣寫道:從列表框中獲取信息Windows Phone 8

void downloadData() 
{ 
    // Instance of a WebClient object 
    WebClient downloader = new WebClient(); 

    // EventHandler for download String completed 
    downloader.DownloadStringCompleted += new DownloadStringCompletedEventHandler(downloaded); 

    // AsyncDownloading of the Websitecontent and Encoding in UTF8 
    downloader.Encoding = Encoding.UTF8; 
    downloader.DownloadStringAsync(new Uri("http://scm1.hensgen.net:8181/cxf/plugins/mandantenmonitor/rs/list", UriKind.Absolute)); 
} 

void downloaded(object sender, DownloadStringCompletedEventArgs e) 
{ 
    // tests wheter string is empty or not downloaded completely 
    if (e.Result == null || e.Error != null) 
    { 
     MessageBox.Show("Error occured while downloading JSON-file from server"); 
    } 
    else 
    { 
     // Deserialize if downloaded succeedes 
     DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(MandantenListeRoot)); 

     // Reads the e.Result string and writes it in UTF8 encoded into a MemoryStream and Cast it from type object to MandantenListeRoot 
     MandantenListeRoot root = serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(e.Result))) as MandantenListeRoot; 
     MandantenListe mandListe = root.mandantenListeDataMember; 

     // Bind the downloaded Collection to our MandantenListBox-control Panel 
     mandantenListeBox.ItemsSource = mandListe.MandantenCollection; 
    } 

} 

我想分析和這個集合在我了下頁的ID屬性,當我在一個列表條目點擊。 現在我瞭解這一羣在管,如果我理解正確,我應該只是能夠投在MOUSEBUTTONDOWN方法發送對象,並把它傳遞到下一個頁面像這樣

private void MandantenStackPanel_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     //MandantenListeMandant mandant = (sender as Button).DataContext as MandantenListeMandant 
     // PhoneApplicationService.Current.State["MandantenNummer"] = mandant.MandantenNummer; 
     NavigationService.Navigate(new Uri("/Vorlagen.xaml", UriKind.Relative)); 

    } 

這不似乎工作,如果我讀取調試信息,我得到正確的發件人對象是-1。我的頁面的相關XAML如下所示:

<ListBox x:Name="mandantenListeBox" Margin="0,0,0,0"> 
       <ListBox.ItemTemplate> 
        <DataTemplate > 
         <StackPanel Margin="0,10,0,10" Width="455" MouseLeftButtonDown="MandantenStackPanel_MouseLeftButtonDown"> 
          <TextBlock Text="{Binding MandantenNummer}" FontSize="24" TextWrapping="Wrap"/> 
          <TextBlock Text="{Binding MandantenBezeichnung}" FontSize="24" TextWrapping="Wrap"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 

感謝您的幫助,我真的很感激它。

回答

0

你應該這樣做,如Web請求,那麼這將是這樣的:

NavigationService.Navigate(new Uri("/Chatting.xaml?SelectedIndex="+selectedItemID.ToString(), UriKind.Relative)); 

然後檢索它的PAE你瀏覽過:

protected override void OnNavigatedTo(NavigationEventArgs e) 
    { 
     base.OnNavigatedTo(e); 
     try 
     { 
      selectedItemID = Convert.ToInt32(this.NavigationContext.QueryString["SelectedIndex"]); 
     } 
     catch 
     { 
      MessageBox.Show("An error occured finding selecteditem", "Error", MessageBoxButton.OK); 
     } 
    } 
+1

這並沒有回答我的預期問題,而是因爲我發現這只是一個錯誤的演員。應該是堆疊面板而不是按鈕。然而,您的解決方案對我的下一個問題非常有幫助,所以非常感謝。 – Blue