c# - 在 UWP 的 gridview 中检索 JSON 数据

标签 c# json xaml gridview uwp

有两个页面从两个链接 json 中检索数据。 Page1 显示带有链接的 JSON 数据:http://.../mobileapp/GetCategoriesXMLa Page2 显示带有链接的 JSON 数据:http://.../mobileapp/GetPostByCategoryXMLa?term_id=(item_id)

我发现很难检索第一页所选菜单的数据id来完成网站链接(第二页的数据)。 示例 JSON Page1: 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>

Page1代码:

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

示例显示第1页: 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>

Page2代码:

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: Page2

家具类:

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

        public string Title { get; set; }

        public string Gambar { get; set; }

        public string Deskripsi { get; set; }
    }

家具类:

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

        public string Name { get; set; }

        public string Menu { get; set; }
    }

因此,用户可以选择 Page1 上的项目,以便能够查看项目的详细信息。例如,用户在Page1 上选择“”,它将在page2 上显示该项目的菜单。 如何检索 page1 上的项目 id 以补充 Page2 的网址? 注意:item_id 用来完成page2 链接上的链接是page1 JSON 上的“id”。 已完成链接页面 2 的示例:http://..../mobileapp/GetPostByCategoryXMLa?term_id=378 378是用户在Page1上选择的“id”项

最佳答案

在第二页你期望 Furniture 但在第一页你设置 FurnitureCategory 改变第一页的 ItemView_ItemClick 如下

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;

关于c# - 在 UWP 的 gridview 中检索 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837894/

相关文章:

c# - 如何让 Null Coalesce 运算符在 ASP.NET MVC Razor 中工作?

json - 在 package.json 中创建子文件夹

java - json序列化时排除字段

C#/XAML 添加图像到按钮

c# - Win8 Metro 为表格数据构建列

c# - 如何修剪 IP 地址以获得它的前 3 部分?

c# - 带 FileHelpers 的条件记录类型

c# - 'this' 在 C# 构造函数中意味着什么?

c# - 将数据表转换为 WCF 中的列表

WPF 边框 ZIndex 未按预期工作