我想让ListView拥有我的音乐库及其名称,艺术家,专辑,专辑封面,我通过make Class将其命名为MusicLibs.cs并编写此代码 在MusicLibs.cs中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;
namespace App29
{
class MusicLib
{
public string FileName { get; set; }
public string Artist { get; set; }
public string Album { get; set; }
public TimeSpan Duration { get; set; }
public string MusicPath{ get; set; }
public BitmapImage AlbumCover { get; set; }
}
}
我在MainPage.xaml中写道:
<Page
x:Class="App29.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App29"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView Name="mylist" ItemsSource="{x:Bind MusicList}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Tapped="StackPanel_Tapped" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FileName}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Artist}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Album}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Duration}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FileName}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Image Source="{Binding AlbumCover}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<MediaElement Name="mymediaelememt" Source="{Binding MusicPath}"/>
</Grid>
我在MainPage.xaml.cs中写这个:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Background;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.FileProperties;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
namespace App29
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
private ObservableCollection<MusicLib> MusicList = new ObservableCollection<MusicLib>();
public MainPage()
{
this.InitializeComponent();
}
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
StorageFolder musicLib = KnownFolders.MusicLibrary;
var files = await musicLib.GetFilesAsync();
foreach (var file in files)
{
StorageItemThumbnail currentThumb = await file.GetThumbnailAsync(ThumbnailMode.MusicView, 200, ThumbnailOptions.UseCurrentScale);
var albumCover = new BitmapImage();
albumCover.SetSource(currentThumb);
var musicProperties = await file.Properties.GetMusicPropertiesAsync();
var musicname = musicProperties.Title;
var musicdur = musicProperties.Duration;
var artist = musicProperties.Artist;
if (artist == "")
{
artist = "Artista desconocido";
}
var album = musicProperties.Album;
if (album == "")
{
album = "Unkown";
}
MusicList.Add(new MusicLib
{
FileName = musicname,
Artist = artist,
Album = album,
Duration = musicdur,
AlbumCover = albumCover,
MusicPath = file.Path
});
}
}
private void StackPanel_Tapped(object sender, TappedRoutedEventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
}
并且它工作正常,但我希望它能够播放选定的音乐,当我点击它我是怎么做的???
答案 0 :(得分:0)
您可以为ListView创建一个新的SelectionChanged事件:
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListView view = (ListView)sender;
//Get Selected Item
MusicLib song = view.SelectedItem as MusicLib;
string path = song.MusicPath;
//code to play song from song.MusicPath
MediaElement player = new MediaElement();
player.Source = MediaSource.CreateFromUri(new Uri(path));
player.Play();
//can then control the playback of the song through the player object then
}
然后将其附加到构造函数中的ListView,以获取已触发事件的页面:
mylist.SelectionChanged += ListView_SelectionChanged;
编辑1: 添加了从源字符串中播放歌曲
答案 1 :(得分:0)
要将ListView用作可点击列表,您应该使用ItemClick
事件。
<强> XAML:强>
<ListView SelectionMode="None"
IsItemClickEnabled="True"
ItemClick="ListView_ItemClick"
>
<强> C#:强>
private void ListView_ItemClick(object sender, ItemClickEventArgs e)
{
MusicLib clicked = (MusicLib)e.ClickedItem;
// Playing the file
}
答案 2 :(得分:0)
首先,您应该使用Marian的方法或@Kenny方法来获取用户点击的项目,然后您应该使用MediaPlayer
来帮助您播放音乐并为其命名,比如说它&#34; Media&#34; 。执行此操作后,您可以将Meia.Source
设置为用户选择的音乐。
Meia.Source=MediaSource.CreateFromUri(new Uri(song.MusicPath));
Media.Play();
有关MediaPlayer
的更多信息,请转到https://msdn.microsoft.com/en-us/windows/uwp/audio-video-camera/play-audio-and-video-with-mediaplayer