我想在Windows Phone 8中使用语言c#后面的代码制作音乐播放器并在XAML中进行设计。我想在一个窗口中显示手机的mp3文件列表。
如何在一个窗口中显示手机的mp3文件列表?
答案 0 :(得分:0)
好吧,如果您想列出手机媒体库中的歌曲列表,那么
制作基类SongModel
public class SongModel
{
public int songId { get; set; }
public string songName { get; set; }
}
制作全局变量
MediaLibrary mLibrary = new MediaLibrary();
SongCollection songs;
List<SongModel> songnames;
现在在页面构造函数中编写这些代码行
songs = mLibrary.Songs;
MediaPlayer.ActiveSongChanged += MediaPlayer_ActiveSongChanged;
MediaPlayer.MediaStateChanged += MediaPlayer_MediaStateChanged;
if (songs.Count != 0)
{
songnames = new List<SongModel>();
for (int i = 0; i < songs.Count; i++)
{
songnames.Add(new SongModel() { songName = songs[i].Name, songId = i });
}
lbMusic.ItemsSource = songnames; // lbMusic is the list/listbox here
}
xaml for listbox lbMusic
<ListBox SelectionChanged="lbMusic_SelectionChanged" Foreground="Black" FontWeight="Bold" Name="lbMusic" Height="210" Width="480" Margin="0,10,0,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,5,0,5" Width="480" Background="White" Height="40">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Text="{Binding songName}" TextTrimming="WordEllipsis" Margin="30,0,0,0" Tag="{Binding songId}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
根据歌曲ID播放歌曲,Ovveride列表框的selectionChanged事件。
private void lbMusic_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
if (lbMusic.SelectedIndex != -1)
{
int musicId = (lbMusic.SelectedItem as SongModel).songId;
MediaPlayer.Play(songs, musicId);
}
}
catch
{
MessageBox.Show(TextResources.resErrorActiveSong);
}
}
我希望这有助于预期