来自GridViewItem(UWP)的Retreive stackpanel子代

时间:2017-07-29 22:00:46

标签: c# listview gridview uwp stackpanel

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

为了澄清,我需要能够获取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)));
    }

感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

我想我永远不应该编码累了吧?我自己的问题的答案实际上非常简单

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