简体   繁体   English

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

[英]Getting information out of a listbox windows phone 8

i'm currently developing an app for windows phone 8, that grabs JSON data from the internet, parses it into a collection, binds this collection to a listbox and shows it. 我目前正在为Windows Phone 8开发一个应用程序,该应用程序可以从互联网上获取JSON数据,将其解析为一个集合,然后将该集合绑定到列表框并显示出来。 This works fine, and i do it like this: 这工作正常,我这样做是这样的:

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

}

I want to parse and ID attribute of this collection to the next page in my up when i click on one list entry. 当我单击一个列表条目时,我想将此集合的ID属性解析到我的下一页。 Now i read about this a bunch on the tubes and if i understand it correctly i should simply be able to cast the sender object in the MouseButtonDown method and pass it onto the next page like this 现在,我在管子上读到了一堆,如果我对它的理解是正确的,我应该能够简单地在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));

    }

This does not seem work and if i read the debug information i get correctly the sender object is -1. 这似乎不起作用,并且如果我读取了调试信息,则可以正确获取发送者对象为-1。 The relevant XAML for my page looks like this: 我页面的相关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>

Thank you for your help, i really appreciate it. 谢谢您的帮助,我非常感谢。

You should do it like web request which will then look like this: 您应该像网络请求那样进行操作,然后将如下所示:

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

Then retrieve it on the pae you navigate too: 然后在您也导航的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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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