2015-01-17 112 views
1

我在設備上創建了歌曲列表。如何從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"/> 
+0

你使用MVVM和綁定嗎? – RenDishen

+0

在XAML中使用綁定 – SMM

+0

您的意思是每當選擇一首歌曲時,它應該會自動播放該歌曲? – Kulasangar

回答

0

你需要綁定你的ListView 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綁定到您的MediaElementSource