C# 从列表框中获取信息windows phone 8

C# 从列表框中获取信息windows phone 8,c#,windows-phone-8,listbox,C#,Windows Phone 8,Listbox,我目前正在为WindowsPhone8开发一个应用程序,它从互联网上获取JSON数据,将其解析为一个集合,将该集合绑定到一个列表框并显示出来。这很好,我是这样做的: void downloadData() { // Instance of a WebClient object WebClient downloader = new WebClient(); // EventHandler for download String completed download

我目前正在为WindowsPhone8开发一个应用程序,它从互联网上获取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;
    }

}
 <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>
  NavigationService.Navigate(new Uri("/Chatting.xaml?SelectedIndex="+selectedItemID.ToString(), UriKind.Relative));
当我单击一个列表条目时,我想将此集合的属性和ID解析到我的up中的下一页。 现在我在管道上读了很多关于这方面的内容,如果我理解正确,我应该能够在MouseButtonDown方法中转换sender对象,并像这样将其传递到下一页

  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如下所示:

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

}
 <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>
  NavigationService.Navigate(new Uri("/Chatting.xaml?SelectedIndex="+selectedItemID.ToString(), UriKind.Relative));


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

您应该像web请求一样执行此操作,然后将显示如下:

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

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

这并没有回答我想问的问题,但当我发现这只是一个错误的演员阵容。应该是stackpanel而不是button。不过,你的解决方案在我的下一个问题上帮了我很大的忙,所以非常感谢你。