有两个页面从两个链接 json 中检索数据。 Page1 显示带有链接的 JSON 数据:http://.../mobileapp/GetCategoriesXMLa Page2 显示带有链接的 JSON 数据:http://.../mobileapp/GetPostByCategoryXMLa?term_id=(item_id)
我发现很难检索第一页所选菜单的数据id来完成网站链接(第二页的数据)。 示例 JSON Page1:
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);
}
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();
}
}
}
家具类:
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/