1

I new to WP 8.1. I am trying to show Music Folders from a Windows Phone 8.1 inside WrapGrid. I am able to access the Folders but I am unable to display them as Folders in the XAML design I have built.

Here's my XAML Code:

<Button x:Name="dumybutton" Content="Click me to see Folders" Background="#FF6E5FCF" Click="dumyclick"/>
<Grid x:Name="ShowFolders" Background="#FFEA8282">
   <ScrollViewer>
      <ListView x:Name="ViewMusicFolders" Grid.Row="1" Grid.Column="2" VirtualizingStackPanel.VirtualizationMode="Recycling" SelectionMode="None" IsActiveView="True">                                    
         <ListView.ItemsPanel>
            <ItemsPanelTemplate>
               <WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="2" />
            </ItemsPanelTemplate>
         </ListView.ItemsPanel>
      </ListView>
   </ScrollViewer>
</Grid>

My C# code:

private void dumyclick(object sender, TappedRoutedEventArgs e)
{
   ViewMusicFolders.Opacity = 1;
   GenerateFolders();
}

private async void GenerateFolders()
{
   try
   {
      // To get all music folders
      IReadOnlyList<IStorageItem> MusicFolders = await KnownFolders.MusicLibrary.GetFoldersAsync();                
      SeeFolders(MusicFolders);
   }
   catch {}
}

private async void SeeFolders(IReadOnlyList<IStorageItem> MusicFolderList)
{
   try
   {

      foreach(IStorageItem mItem in MusicFolderList)
      {
         IStorageItem item = mItem;
         int temp = 0;

         // Checks if the item is a Folder
         if(item.IsOfType(Windows.Storage.StorageItemTypes.Folder))
         {                       
            StorageFolder mFolder = (StorageFolder)item;

            // To get all Items (Files & Folders) present in the folder
            IReadOnlyList<IStorageItem> fileList = await mFolder.GetItemsAsync();

            // checks the count. If folder contains any files or sub-folders, fetch details & then traverse through the fileList.
            if(fileList.Count >0)
            {
               // create object of MusicAlbums() class.
               MusicF musicAlbumObj = new MusicF();

               // set name of item Folder.
               musicAlbumObj.strName = item.Name;

               // set path of item Folder.
               musicAlbumObj.strPath = item.Path;


               string showText = "";
               showText = musicAlbumObj.strName + " *** " + musicAlbumObj.strPath;
               MessageDialog msg = new MessageDialog(showText);
               await msg.ShowAsync(); 
            }                      
         }                 
      }
   }
   catch {}
}

My MusicF Class

public class MusicF
{
   public string strName { get; set; }
   public string strPath { get; set; }
}
Sajeev C
  • 1,538
  • 4
  • 17
  • 30

1 Answers1

2

For example, if you want to show the folder names and paths you should do the following:

In your xaml change to the following:

<Button x:Name="dumybutton" Content="Click me to see Folders" Background="#FF6E5FCF" Click="dumyclick"/>
<Grid x:Name="ShowFolders" Background="#FFEA8282">
   <ScrollViewer>
      <ListView x:Name="ViewMusicFolders" Grid.Row="1" Grid.Column="2" VirtualizingStackPanel.VirtualizationMode="Recycling" SelectionMode="None" IsActiveView="True">                                    
         <ListView.ItemsPanel>
            <ItemsPanelTemplate>
               <WrapGrid Orientation="Horizontal" MaximumRowsOrColumns="2" />
            </ItemsPanelTemplate>
         </ListView.ItemsPanel>
         <ListView.ItemTemplate>
            <DataTemplate>
               <StackPanel>
                 <TextBlock Text="{Binding strName}" />
                 <TextBlock Text="{Binding strPath}" />
               </StackPanel>
            </DataTemplate>
         </ListView.ItemTemplate>
      </ListView>
   </ScrollViewer>
</Grid>

And try to apply the following changes in your xaml.cs

private async void SeeFolders(IReadOnlyList<IStorageItem> MusicFolderList)
{

   List<MusicF> foldersList = new List<MusicF>();

   try
   {

      foreach(IStorageItem mItem in MusicFolderList)
      {
         IStorageItem item = mItem;
         int temp = 0;

         // Checks if the item is a Folder
         if(item.IsOfType(Windows.Storage.StorageItemTypes.Folder))
         {                       
            StorageFolder mFolder = (StorageFolder)item;

            // To get all Items (Files & Folders) present in the folder
            IReadOnlyList<IStorageItem> fileList = await mFolder.GetItemsAsync();

            // checks the count. If folder contains any files or sub-folders, fetch details & then traverse through the fileList.
            if(fileList.Count >0)
            {
               // create object of MusicAlbums() class.
               MusicF musicAlbumObj = new MusicAlbums();

               // set name of item Folder.
               musicAlbumObj.strName = item.Name;

               // set path of item Folder.
               musicAlbumObj.strPath = item.Path;

               foldersList.Add(musicAlbumObj);


               string showText = "";
               showText = musicAlbumObj.strName + " *** " + musicAlbumObj.strPath;
               MessageDialog msg = new MessageDialog(showText);
               await msg.ShowAsync(); 
            }                      
         }                 
      }

      ViewMusicFolders.ItemsSource = foldersList;

   }
   catch {}
}

The pieces of code I've added are which was missing to link your code behind folder listing to your xaml.cs. I hope this solves the problem.

meneses.pt
  • 2,588
  • 3
  • 27
  • 38