2014-11-24 21 views
1

我是WP 8.1的新手。我試圖從WrapGrid中的Windows Phone 8.1中顯示音樂文件夾。我能夠訪問文件夾,但我無法將它們顯示爲我構建的XAML設計中的文件夾。無法在WP 8.1中的WrapGrid中顯示文件夾?

這裏是我的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; } 
} 

回答

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.我希望這能解決問題。

+0

此代碼沒有顯示任何文件夾。 @Meneses – 2014-11-25 05:57:16

+0

@WD你能否確認我的foldersList對象在foreach結束時有項嗎? – Meneses 2014-11-25 10:04:34

相關問題