无法在WP 8.1中的WrapGrid中显示文件夹?

时间:2014-11-24 06:48:04

标签: c# xaml windows-runtime winrt-xaml windows-phone-8.1

我是WP 8.1的新手。我试图在WrapGrid中显示Windows Phone 8.1中的音乐文件夹。我能够访问文件夹,但我无法在我构建的XAML设计中将它们显示为文件夹。

这是我的XAML代码:

<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>

我的C#代码:

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 {}
}

我的MusicF课程

public class MusicF
{
   public string strName { get; set; }
   public string strPath { get; set; }
}

1 个答案:

答案 0 :(得分:2)

例如,如果要显示文件夹名称和路径,则应执行以下操作:

在您的xaml中更改为以下内容:

<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>

并尝试在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 {}
}

我添加的代码片段缺少将文件夹列表后面的代码链接到xaml.cs。我希望这能解决问题。

相关问题