使用来自Web服务的json数据在Windows Phone应用程序中创建主 - 详细视图

时间:2014-07-06 14:10:11

标签: c# wpf json xaml

我想选择一个列表框项目,并在文本块的下一页显示其详细信息。这是我的代码。请帮助我或提供一些我可以参考的链接......提前致谢。

这是我的母版页面xaml。

<ListBox x:Name="lstUniversity" Height="582" SelectionMode="Single" FontSize="30"     VerticalAlignment="Bottom">
          <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="1">
                    <Button Name="btn" Content="{Binding NAME}" Click="click" BorderThickness="1"  HorizontalContentAlignment="Left"
                        Height="75" Width="460" FontSize="24" 
                        HorizontalAlignment="Stretch"
                        CommandParameter="{Binding}" Foreground="White"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

主页的c#代码

public class University
{
    public string UID { get; set; }
    public string NAME { get; set; }
    public string RANK { get; set; }
    public string FEES { get; set; }
    public string APPLIEDFEES { get; set; }
    public string CITY { get; set; }
    public string STATE { get; set; }
}

protected override void OnNavigatedTo(NavigationEventArgs args)
{
   try
   {
      WebClient webClient = new WebClient();
      Uri uri = new Uri("http://www.mastertionary.com/ios/connect1.php");
      webClient.DownloadStringCompleted += new         DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
  webClient.DownloadStringAsync(uri);
   }
   catch (Exception ex)
   {
      MessageBox.Show(ex.Message);
   }
}     

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    var rootObject = JsonConvert.DeserializeObject<List<University>>(e.Result);
        lstUniversity.ItemsSource = rootObject;
}

public void click(object sender, RoutedEventArgs e)
{   
  NavigationService.Navigate(new Uri("/details.xaml", UriKind.Relative));
}

我的详细信息页面xaml是

<ListBox x:Name="unidetails" Height="582" SelectionMode="Single" FontSize="30" VerticalAlignment="Bottom">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="1">
                    <StackPanel><TextBlock Text="{Binding NAME}" Height="100" Width="300"/>
                    <TextBlock Text="{Binding RANK}" Height="100" Width="300"/>
                    <TextBlock Text="{Binding FEES}" Height="100" Width="300"/>
                    <TextBlock Text="{Binding APPLIEDFEES}" Height="100" Width="300"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

请帮助我如何根据母版页中选择的项目显示等级,费用等详细信息?

1 个答案:

答案 0 :(得分:0)

  1. 您可以将对象存储在PhoneApplicationState中,并在下一页中检索它。

      if (PhoneApplicationService.Current.State.ContainsKey("ApplicationDataObject"))
      {
        // If it exists, assign the data to the application member variable.
        ApplicationDataStatus = "data from preserved state.";
        ApplicationDataObject = PhoneApplicationService.Current.State["ApplicationDataObject"] as University;
      }
    

    参考:http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff967547(v=vs.105).aspx

  2. 或者,使用可以从两个页面引用的静态对象。

    public static University UnivObject;

  3. 如果参数不多,请在查询参数中发送数据,如: 比如说,如果您已将列表框与列表绑定,则可以访问列表框中的所选项目点击事件:

    University univObj = listbox.SelectedItem;
    
    NavigationService.Navigate(new Uri("/details.xaml?UID=" + univObj.UID + "&NAME=" + univObj.Name + "&RANK=" + univObj.Rank,UriKind.Relative));
    
  4. 您可以在下一页中检索值:

    NavigationContext.QueryString["UID"].ToString;