2016-11-28 74 views
1

有兩個頁面從兩段鏈接json中檢索數據。 Page1顯示帶有鏈接的JSON數據:http://.../mobileapp/GetCategoriesXMLa和Page2顯示帶有鏈接的JSON數據:http://.../mobileapp/GetPostByCategoryXMLa?term_id=(item_id)在UWP的gridview中檢索JSON數據

我發現很難檢索第一頁上選定菜單的數據ID以完成網站鏈接(數據爲第二頁)。 例JSON第1頁: JSON Page1

例JSON第二頁: JSON page2

XAML第1頁:

<GridView 
       x:Name="itemGridView" 
       AutomationProperties.AutomationId="ItemDetailScrollViewer" 
       Padding="20,0,0,0" 
       Margin="30,30,30,30" 
       SelectionMode="None" 
       IsSwipeEnabled="false" 
       IsItemClickEnabled="True" 
       ItemClick="ItemView_ItemClick" 
       DataContext="{Binding SelectedItem, ElementName=itemListView}" 
       ScrollViewer.HorizontalScrollMode="Auto" ScrollViewer.VerticalScrollMode="Auto" 
       ScrollViewer.ZoomMode="Disabled"> 

       <GridView.ItemTemplate> 
        <DataTemplate> 
         <Grid Height="200" Width="200" Margin="10,10,0,0" Background="{x:Null}" BorderBrush="#FF7A7A7A" BorderThickness="1"> 
          <Image x:Name="menu" Source="{Binding Menu}" HorizontalAlignment="Center" Stretch="Uniform" AutomationProperties.Name="{Binding Name}" ImageOpened="Image_ImageOpened" Loaded="cover_Loaded" Loading="cover_Loading"/> 
          <StackPanel x:Name="stackJudul" Margin="0,-25,0,0" Height="25" VerticalAlignment="Bottom" Background="#CC7A7A7A"> 
           <TextBlock x:Name="name" Text="{Binding Name}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="White" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="SemiBold"/> 
          </StackPanel> 
          <TextBlock x:Name="ID" Text="{Binding ID}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="White" FontSize="17" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="SemiBold" Visibility="Collapsed"/> 
         </Grid> 
        </DataTemplate> 
       </GridView.ItemTemplate> 
      </GridView> 

第1頁代碼:

private async void Store() 
     { 
      try 
       { 
        var httpClient = new HttpClient(new HttpClientHandler()); 
        string urlPath = "http://..../mobileapp/GetCategoriesXMLa"; 

        var values = new List<KeyValuePair<string, string>> 
       { 

       }; 

        HttpResponseMessage response = await httpClient.PostAsync(urlPath, new FormUrlEncodedContent(values)); 
        response.EnsureSuccessStatusCode(); 

        if (!response.IsSuccessStatusCode) 
        { 
         loading.IsActive = false; 
         RequestException(); 
        } 

        string jsonText = await response.Content.ReadAsStringAsync(); 
        JsonArray jsonData1 = JsonArray.Parse(jsonText); 

        foreach (JsonValue groupValue in jsonData1) 
        { 

         JsonObject groupObject = groupValue.GetObject(); 

         string menuId = groupObject["id"].GetString(); 
         string title = groupObject["name"].GetString(); 
         string button = groupObject["thumbnail-200x200"].GetString(); 

         FurnitureHome file = new FurnitureHome(); 
         file.ID = menuId; 
         file.Menu = button; 
         file.Name = title; 

         datasource.Add(file); 
        } 

        if (jsonData1.Count > 0) 
        { 
         itemGridView.ItemsSource = datasource; 
        } 
        else 
        { 
         loading.IsActive = false; 
         statusKosong.Visibility = Visibility.Visible; 
        } 
       } 
       catch (HttpRequestException ex) 
       { 
        loading.IsActive = false; 
        RequestException(); 
       } 
      } 
     } 
private void ItemView_ItemClick(object sender, ItemClickEventArgs e) 
     { 
      FurnitureHome item = e.ClickedItem as FurnitureHome; 
      Furniture itemDetail = new Furniture(); 
      DetailId.Text = item.ID; 
      itemDetail.ID = DetailId.Text; 
      this.Frame.Navigate(typeof(FurnitureCategory), itemDetail.ID); 
     } 

實施例顯示PAGE1: Page1

XAML第2頁:

<GridView 
       x:Name="itemGridView" 
       AutomationProperties.AutomationId="ItemDetailScrollViewer" 
       Padding="20,0,0,0" 
       Margin="30,30,30,30" 
       SelectionMode="None" 
       IsSwipeEnabled="false" 
       IsItemClickEnabled="True" 
       ItemClick="ItemView_ItemClick" 
       DataContext="{Binding SelectedItem, ElementName=itemListView}" 
       ScrollViewer.HorizontalScrollMode="Auto" ScrollViewer.VerticalScrollMode="Auto" 
       ScrollViewer.ZoomMode="Disabled"> 

       <GridView.ItemTemplate> 
        <DataTemplate> 
         <Grid Height="140" Width="200" Margin="10,10,0,0" Background="{x:Null}" BorderBrush="#FF7A7A7A" BorderThickness="1"> 
          <Image x:Name="menu" Source="{Binding Gambar}" HorizontalAlignment="Center" Stretch="Uniform" AutomationProperties.Name="{Binding Title}" ImageOpened="Image_ImageOpened" Loaded="cover_Loaded" Loading="cover_Loading"/> 
          <StackPanel x:Name="stackJudul" Margin="0,-25,0,0" Height="25" VerticalAlignment="Bottom" Background="#CC7A7A7A"> 
           <TextBlock x:Name="name" Text="{Binding Title}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="White" FontSize="15" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="SemiBold"/> 
          </StackPanel> 
          <TextBlock x:Name="ID" Text="{Binding ID}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" Foreground="White" FontSize="17" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="SemiBold" Visibility="Collapsed"/> 
         </Grid> 
        </DataTemplate> 
       </GridView.ItemTemplate> 
      </GridView> 

第二頁編碼:

protected override void OnNavigatedTo(NavigationEventArgs e) 
     { 
      Furniture detail = e.Parameter as Furniture; 
      StoreDetail(); 
     } 
private async void StoreDetail() 
     { 
       try 
       { 
        Furniture detail = new Furniture(); 

        var httpClient = new HttpClient(new HttpClientHandler()); 
        string urlPath = "http://indonesia-furniture.com/mobileapp/GetPostByCategoryXMLa?term_id=378"; 

        var values = new List<KeyValuePair<string, string>> 
        { 

        }; 

        HttpResponseMessage response = await httpClient.PostAsync(urlPath, new FormUrlEncodedContent(values)); 
        response.EnsureSuccessStatusCode(); 

        if (!response.IsSuccessStatusCode) 
        { 
         loading.IsActive = false; 
         RequestException(); 
        } 

        string jsonText = await response.Content.ReadAsStringAsync(); 
        JsonObject jsonObject = JsonObject.Parse(jsonText); 
        JsonArray jsonData1 = jsonObject["posts"].GetArray(); 

        foreach (JsonValue groupValue in jsonData1) 
        { 

         JsonObject groupObject = groupValue.GetObject(); 

         double menuId = groupObject["post_id"].GetNumber(); 
         string title = groupObject["post_title"].GetString(); 
         string image = groupObject["featured_image"].GetString(); 

         Furniture file = new Furniture(); 
         file.ID = menuId.ToString(); 
         file.Title = title; 
         file.Gambar = image; 

         datasource.Add(file); 
        } 

        if (jsonData1.Count > 0) 
        { 
         itemGridView.ItemsSource = datasource; 
        } 
        else 
        { 
         loading.IsActive = false; 
         statusKosong.Visibility = Visibility.Visible; 
        } 
       } 
       catch (HttpRequestException ex) 
       { 
        loading.IsActive = false; 
        RequestException(); 
       } 
      } 
     } 

實施例顯示第二頁: Page2

傢俱類:

class Furniture 
    { 
     public string ID { get; set; } 

     public string Title { get; set; } 

     public string Gambar { get; set; } 

     public string Deskripsi { get; set; } 
    } 

FurnitureHome class:

class FurnitureHome 
    { 
     public string ID { get; set; } 

     public string Name { get; set; } 

     public string Menu { get; set; } 
    } 

因此,用戶可以選擇Page1上的項目以便能夠看到該項目的細節。例如,用戶在Page1上選擇「」,它將在第2頁上顯示該項目的菜單。 如何檢索第1頁上的項目的ID以補充Page2的網站地址? 注意:用於完成第2頁鏈接上鍊接的item_id是第1頁JSON上的「id」。 示例完成鏈路第2頁:http://..../mobileapp/GetPostByCategoryXMLa?term_id=378 378是「ID」項目是由用戶在第二頁中選擇第一頁上

+0

在你的代碼中,'e.Parameter'在第2頁是id。 – tao

回答

0

你是希望Furniture但在第1頁設置FurnitureCategory變化ItemView_ItemClick在第1頁如下

private void ItemView_ItemClick(object sender, ItemClickEventArgs e) 
{ 
    FurnitureHome item = e.ClickedItem as FurnitureHome; 
    Furniture itemDetail = new Furniture(); 
    DetailId.Text = item.ID; 
    itemDetail.ID = DetailId.Text; 
    this.Frame.Navigate(typeof(Furniture), itemDetail); 
} 

在第二頁

protected override void OnNavigatedTo(NavigationEventArgs e) 
{ 
    Furniture detail = e.Parameter as Furniture; 
    StoreDetail(detail.ID);//send id 
} 
private async void StoreDetail(int id) // method change to accept id 
{ 
    try 
    { 
     Furniture detail = new Furniture(); 

     var httpClient = new HttpClient(new HttpClientHandler()); 
     //build url with given id 
     string urlPath = "http://indonesia-furniture.com/mobileapp/GetPostByCategoryXMLa?term_id="+id;