从 GridView 项目 (UWP) 中检索堆栈面板 children

Retreive stackpanel children from GridViewItem (UWP)

我想知道如何获取已添加到 GridViewItem 的堆栈面板的内容(YouTube 数据 API,堆栈面板包含缩略图的位图图像、视频的标题和带有视频 ID 的隐藏 TextBlock 以供参考)。

澄清一下,我需要能够获取包含在 GridViewItem 中的 StackPanel 的内容(例如那个隐藏的 TextBox 的字符串),这样我才能使用数据来显示正确的视频。

这是我用来创建堆栈面板的代码。

    public async Task SearchByKeyword(string searchTerm)
    {
        GeneralFunctions gf = new GeneralFunctions();
        var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            ApiKey = gf.YouTubeAPIKey,
            ApplicationName = this.GetType().ToString()
        });

        var searchListRequest = youtubeService.Search.List("snippet");
        searchListRequest.Q = searchTerm; // Replace with your search term.
        searchListRequest.MaxResults = 50;
        searchListRequest.SafeSearch = SearchResource.ListRequest.SafeSearchEnum.Strict;

        // Call the search.list method to retrieve results matching the specified query term.
        var searchListResponse = await searchListRequest.ExecuteAsync();

        List<string> videos = new List<string>();
        List<string> channels = new List<string>();
        List<string> playlists = new List<string>();

        // Add each result to the appropriate list, and then display the lists of
        // matching videos, channels, and playlists.
        foreach (var searchResult in searchListResponse.Items)
        {
            switch (searchResult.Id.Kind)
            {
                case "youtube#video":

                    // Create a new StackPanel to insert as a ListViewItem
                    StackPanel sPanel = new StackPanel();
                    sPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
                    sPanel.VerticalAlignment = VerticalAlignment.Stretch;
                    sPanel.Orientation = Orientation.Vertical;
                    sPanel.Width = 160;
                    sPanel.Height = 160;

                    // Create new StackPanel "Child" elements with alignment and width
                    TextBlock tb1 = new TextBlock();
                    tb1.Text = searchResult.Snippet.Title;
                    tb1.TextWrapping = TextWrapping.WrapWholeWords;
                    tb1.Width = 160;

                    // Create new StackPanel "Child" elements with alignment and width
                    TextBlock tb2 = new TextBlock();
                    tb2.Text = searchResult.Snippet.ChannelId;
                    tb2.TextWrapping = TextWrapping.WrapWholeWords;
                    tb2.Width = 160;

                    // Create new StackPanel child element for a 120x120 thumbnail of the videos from the search results
                    Image image = new Image();
                    image.Source = new BitmapImage(new Uri(searchResult.Snippet.Thumbnails.Default__.Url, UriKind.Absolute));

                    // Add a "hidden" child element to each stackpanel to hold the video identity
                    TextBlock h1 = new TextBlock();
                    h1.Text = searchResult.Id.VideoId.ToString();
                    h1.Visibility = Visibility.Collapsed;
                    h1.TextWrapping = TextWrapping.WrapWholeWords;

                    sPanel.Children.Add(image);
                    sPanel.Children.Add(tb1);
                    sPanel.Children.Add(tb2);
                    sPanel.Children.Add(h1);

                    SearchResults.Items.Add(sPanel);

                    break;

                case "youtube#channel":
                    //SearchResults.Items.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
                    break;

                case "youtube#playlist":
                    //playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
                    break;
            }
        }



        //Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos)));
        //Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels)));
        //Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists)));
    }

感谢任何帮助。

谢谢

我想我应该永远不会对代码感到厌倦,对吧?我自己的问题的答案其实非常非常简单

        if (SearchResults.SelectedItems.Count > 0)
        {
            StackPanel sp = (StackPanel)SearchResults.SelectedItem;

            TextBlock tb1 = (TextBlock)sp.Children[1];
            TextBlock tb2 = (TextBlock)sp.Children[2];
            TextBlock hf1 = (TextBlock)sp.Children[3];
        }