从列表框Windows Phone中获取信息8

时间:2013-12-14 18:08:27

标签: c# windows-phone-8 listbox

我正在开发一款适用于Windows Phone 8的应用程序,该应用程序从互联网上抓取JSON数据,将其解析为一个集合,将此集合绑定到列表框并显示它。这很好用,我这样做:

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>

感谢您的帮助,我真的很感激。

1 个答案:

答案 0 :(得分:0)

你应该像网页请求一样,这将是这样的:

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