从列表框中获取信息windows phone 8
本文关键字:windows phone 信息 获取 列表 | 更新日期: 2024-09-24 05:42:04
我目前正在为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属性解析到我的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如下所示:
<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>
谢谢你的帮助,我真的很感激。
您应该像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);
}
}