2017-07-29 12 views
0

GridViewItemに追加されたStackPanelのコンテンツをどのように取得できるのでしょうか(YouTubeデータAPI、スタックパネルにはサムネイルのビットマップイメージ、ビデオと参照用のビデオIDを持つ非表示TextBlock)。GridViewItem(UWP)のStackPick子どもを取得する

明らかにするには、正しいビデオを表示するためにデータを使用できるように、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))); 
    } 

助けていただければ幸いです。私は疲れ権利をコーディングすることはありません推測

おかげ

+0

であるあなたは見たことがあり、[この](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]; 
     } 
関連する問題