2017-07-29 90 views
0

我想知道如何獲得已添加到GridViewItem(YouTube數據API,堆棧面板包含縮略圖的位圖圖像,視頻和帶有視頻ID的隱藏TextBlock以供參考)。從GridViewItem(UWP)Retreive stackpanel兒童

爲了澄清,我需要能夠獲取GridViewItem中包含的StackPanel的內容(例如該隱藏文本框的字符串),以便我可以使用這些數據來顯示正確的視頻。

下面是我用來創建堆棧面板的代碼。

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

任何幫助表示讚賞。

謝謝

+0

你見過[this](https://stackoverflow.com/questions/16375375/how-do-i-access-a-control-inside- A-XAML的DataTemplate中)? –

回答

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