locked
failed to bind music files to listview

    Question

  • i am trying to bind music files from music library to a listview but i am unsuccesful, my code is below, please tell me where i went wrong

     var folders = Windows.Storage.KnownFolders.MusicLibrary;
                var files = await folders.GetFilesAsync();
                foreach(StorageFile myf in files){
                    mylist.Items.Add(myf);
                }


    Wednesday, January 07, 2015 2:57 PM

Answers

  • Try this

     <Grid>
            <ListView x:Name="list" ItemsSource="{Binding}">
                <ListView.ItemTemplate >
                    <DataTemplate >
                        <Border Margin="6" Background="YellowGreen" >
                               <TextBlock Text="{Binding Name}" FontSize="16"/>
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate> 
            </ListView>
        </Grid>

           async protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                var folders = Windows.Storage.KnownFolders.MusicLibrary;
                var files = await folders.GetFilesAsync();
                list.DataContext = files;
            }

    and remember setting the "Music Library Capability" in Package.appxmanifest.


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Wednesday, January 07, 2015 9:35 PM

All replies

  • Try this

     <Grid>
            <ListView x:Name="list" ItemsSource="{Binding}">
                <ListView.ItemTemplate >
                    <DataTemplate >
                        <Border Margin="6" Background="YellowGreen" >
                               <TextBlock Text="{Binding Name}" FontSize="16"/>
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate> 
            </ListView>
        </Grid>

           async protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                var folders = Windows.Storage.KnownFolders.MusicLibrary;
                var files = await folders.GetFilesAsync();
                list.DataContext = files;
            }

    and remember setting the "Music Library Capability" in Package.appxmanifest.


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Wednesday, January 07, 2015 9:35 PM
  • your solution is working but i want to load mp3 files from music library or other location after clicking a button, it it possible ? 
    Thursday, January 08, 2015 4:02 AM
  • Hi kranthi88,

    Please post another question on the forum for your second question.

    I think its possible but base on you have the privilege to access the folder, and then you can get the file extension to determine if the music file is mp3 format.

    --James


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 21, 2015 8:11 AM
    Moderator