我正在为UWP制作一个简单的音乐播放器应用程序,我有几个问题。 首先,这是我的代码
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
StorageFolder folder = KnownFolders.MusicLibrary;
await RetrieveFilesInFolders(folder);
}
private async Task RetrieveFilesInFolders(StorageFolder parent)
{
foreach (var file in await parent.GetFilesAsync())
{
if (file.FileType == ".mp3")
{
var songProperties = await file.Properties.GetMusicPropertiesAsync();
var currentThumb = await file.GetThumbnailAsync(ThumbnailMode.MusicView, 200, ThumbnailOptions.UseCurrentScale);
var albumCover = new BitmapImage();
albumCover.SetSource(currentThumb);
var song = new Song();
song.Title = songProperties.Title;
song.Artist = songProperties.Artist;
song.Album = songProperties.Album;
song.AlbumCover = albumCover;
song.SongFile = file;
song.FileName = file.Name;
SongUC songUc = new SongUC(song);
sp1.Children.Add(songUc);
}
}
foreach (var folder in await parent.GetFoldersAsync())
{
await RetrieveFilesInFolders(folder);
}
}
用户控制ctor
public SongUC(Song song)
{
this.InitializeComponent();
txtTitle.Text = song.Title;
txtAlbum.Text = song.Album;
txtArtist.Text = song.Artist;
txtName.Text = song.FileName;
imgAlbumArt.Source = song.AlbumCover;
}
RetrieveFilesInFolders
方法是获取所有歌曲的最佳方式。或者,如果音乐文件夹中有大量音乐,它会减慢应用程序的速度答案 0 :(得分:1)
如上所述here,请查看file queries及其ContentsChanged
事件。
答案 1 :(得分:0)
使用FileSystemWatcher:
https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx
它允许您在移动,删除和重命名文件时捕获事件。