在 ItemClick() 事件上获取 ListView 项目的详细信息
Posted
技术标签:
【中文标题】在 ItemClick() 事件上获取 ListView 项目的详细信息【英文标题】:Get ListView item's details on ItemClick() event 【发布时间】:2021-12-14 22:22:50 【问题描述】:我尝试在此处和 microsoft 文档上进行搜索,但找不到针对我的特定查询的解决方案,因为我主要看到了有关如何在 itemclick 上执行操作而不是检索数据的帖子。
我目前正在使用一个 API,它发送一个我反序列化为 2 个部分类的 JSON 请求,我在其中使用一个 foreach 循环将新项目添加到 ListView。您可以在此处查看课程:
public partial class GameListObject
[JsonPropertyName("id")]
public long GameID get; set;
[JsonPropertyName("name")]
public string GameName get; set;
[JsonPropertyName("release_dates")]
public ObservableCollection<ReleaseDate> ReleaseDates get; set;
public partial class ReleaseDate
[JsonPropertyName("id")]
public long Id get; set;
[JsonPropertyName("human")]
public string Human get; set;
以及这里的请求、反序列化和添加到ListView:
//On search box content change
private async void gamehub_search_TextChanged(object sender, TextChangedEventArgs e)
var SearchQuery = gamehub_search.Text;
try
// Construct the HttpClient and Uri
HttpClient httpClient = new HttpClient();
Uri uri = new Uri("https://api.igdb.com/v4/games");
httpClient.DefaultRequestHeaders.Add("Client-ID", App.GlobalClientidIGDB);
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + App.GlobalAccessIGDB);
//Debug.WriteLine("Request Headers: ");
// Construct the JSON to post
HttpStringContent content = new HttpStringContent($"search \"SearchQuery\"; fields name,release_dates.human;");
Debug.WriteLine("Request Contents: " + content);
// Post the JSON and wait for a response
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
uri,
content);
// Make sure the post succeeded, and write out the response
httpResponseMessage.EnsureSuccessStatusCode();
var httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync();
Debug.WriteLine("Request Response: " + httpResponseBody);
//Deserialise the return output into game id, game name and release date
List<GameListObject> gamelistobjects = JsonSerializer.Deserialize<List<GameListObject>>(httpResponseBody);
ObservableCollection<GameListObject> dataList = new ObservableCollection<GameListObject>(gamelistobjects);
ObservableCollection<GameListObject> GameList = new ObservableCollection<GameListObject>();
foreach (var item in dataList)
Debug.WriteLine($"id: item.GameID");
Debug.WriteLine($"name: item.GameName");
GameListObject add = new GameListObject() GameID = item.GameID, GameName = item.GameName ;
GameList.Add(add);
if (item.ReleaseDates != null)
foreach (var date in item.ReleaseDates)
Debug.WriteLine($"releaseDate: date.Human");
gamehub_list.ItemsSource = GameList;
catch (Exception ex)
Debug.WriteLine(ex);
现在我设置了一个 gamehub_list_ItemClick 方法,该方法在 ListView 中的项目被按下时运行。 我想检索该项目中存在的 GameID,因为我需要将它用于用户被重定向到的另一个页面,以便我知道我必须为哪个游戏请求数据。但是,我尝试查找项目的索引并使用类的成员名称来检索它,但我似乎无法让它工作。
ItemClick 方法目前是:
private void gamehub_list_ItemClick(object sender, ItemClickEventArgs e) //When an item in List View is pressed
string clickedItemText = e.ClickedItem.ToString();
Debug.WriteLine("Click Item text: " + clickedItemText);
当我尝试获取项目的索引时,它始终返回为 -1,对于当前 clickedItemText,它返回“单击项目文本:ReviewR.GameHubs+GameListObject”。
我的带有 ListView 的 xaml:
<ListView x:Name="gamehub_list" SelectionMode="None" IsItemClickEnabled="True" ItemClick="gamehub_list_ItemClick" Margin="30,140,44,30" BorderThickness="5" BorderBrush="Black" Background="Gray" RequestedTheme="Dark" Visibility="Collapsed">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock Text="Binding GameID"></TextBlock>
<TextBlock Text="Binding GameName"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
【问题讨论】:
【参考方案1】:点击Item文本:ReviewR.GameHubs+GameListObject
问题是ClickedItem
是对象类型,如果你直接将它传递给字符串,它会像你上面提到的那样返回文本。
对于这种情况,您需要将 ClickedItem
拆箱。
var clickedItem = e.ClickedItem as GameListObject
Debug.WriteLine("Click Item text: " + clickedItem.GameID );
【讨论】:
谢谢你,伙计!我明白这个错误现在感谢你解释它:)以上是关于在 ItemClick() 事件上获取 ListView 项目的详细信息的主要内容,如果未能解决你的问题,请参考以下文章
Listview Itemclick事件未触发android xamarin