如何从列表视图开始播放歌曲?
How to start a song from a listView?
我在设备上创建了一个歌曲列表。
对于每个项目,在我的 ListView 中显示艺术家姓名和歌曲名称。我希望当一首歌曲被选中后,开始播放,我将如何去?
StorageFolder musicLibrary = KnownFolders.MusicLibrary;
IReadOnlyList<StorageFile> musica = await musicLibrary.GetFilesAsync();
if (musica != null)
{
List<Testo> song = new List<Testo>();
{
foreach (StorageFile storage in musica)
{
MusicProperties musicProp = await storage.Properties.GetMusicPropertiesAsync();
song.Add(new Testo
{
NomeArtista = musicProp.Artist,
NomeCanzone = musicProp.Title,
Anno = (int)musicProp.Year,
});
}
}
}
private async void TestiCanzone_ItemClick(object sender, ItemClickEventArgs e)
{
Testo NuovoTesto = e.ClickedItem as Testo;
}
我还创建了一个 MediaElement
<MediaElement x:Name="AudioPlay" Source="" AutoPlay="True"/>
您需要将列表视图的 SelectedValue
绑定到视图模型上的某些 属性:
<ListView SelectedItem="{Binding SelectedAudio, Mode=TwoWay}"/>
在您的视图模型上,您需要处理更改 属性:
public string SelectedCustomMusic
{
get
{
return this.selectedCustomMusic;
}
set
{
if (value != null)
{
this.selectedCustomMusic = value;
this.MusicSource = this.selectedCustomMusic;
base.OnPropertyChanged();
}
}
}
然后将 this.MusicSource
绑定到 MediaElement
的 Source
。
我在设备上创建了一个歌曲列表。
对于每个项目,在我的 ListView 中显示艺术家姓名和歌曲名称。我希望当一首歌曲被选中后,开始播放,我将如何去?
StorageFolder musicLibrary = KnownFolders.MusicLibrary;
IReadOnlyList<StorageFile> musica = await musicLibrary.GetFilesAsync();
if (musica != null)
{
List<Testo> song = new List<Testo>();
{
foreach (StorageFile storage in musica)
{
MusicProperties musicProp = await storage.Properties.GetMusicPropertiesAsync();
song.Add(new Testo
{
NomeArtista = musicProp.Artist,
NomeCanzone = musicProp.Title,
Anno = (int)musicProp.Year,
});
}
}
}
private async void TestiCanzone_ItemClick(object sender, ItemClickEventArgs e)
{
Testo NuovoTesto = e.ClickedItem as Testo;
}
我还创建了一个 MediaElement
<MediaElement x:Name="AudioPlay" Source="" AutoPlay="True"/>
您需要将列表视图的 SelectedValue
绑定到视图模型上的某些 属性:
<ListView SelectedItem="{Binding SelectedAudio, Mode=TwoWay}"/>
在您的视图模型上,您需要处理更改 属性:
public string SelectedCustomMusic
{
get
{
return this.selectedCustomMusic;
}
set
{
if (value != null)
{
this.selectedCustomMusic = value;
this.MusicSource = this.selectedCustomMusic;
base.OnPropertyChanged();
}
}
}
然后将 this.MusicSource
绑定到 MediaElement
的 Source
。