试图在windows phone应用程序8.1上超出音乐文件夹,但出现以下异常:
System.Reflection.TargetInvocationException未经处理消息:System.Windows.ni.dll中发生类型为"System.Reflection.TargetInvocationException"的未经处理的异常。其他信息:调用的目标引发了异常。
这是代码:调用
try
{
GetFiles();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
private async void GetFiles()
{
StorageFolder folder = KnownFolders.MusicLibrary;
IReadOnlyList<StorageFile> listOfFiles;
if (folder!=null)
{
listOfFiles =await folder.GetFilesAsync(); //this line casuing Debugger.break();
}
}
在package.appmanifest文件中添加音乐库功能
然后试试这个代码。它对我有效
StorageFolder folder = KnownFolders.MusicLibrary;
if (folder!=null)
{
var songs = (await folder.GetFilesAsync(Windows.Storage.Search.CommonFileQuery.OrderByName)).ToList();
}
链接到示例代码
试试这个。
适用于Windows 8.1 Universal
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
openPicker.FileTypeFilter.Add(".wav");
openPicker.FileTypeFilter.Add(".mp4");
var file = await openPicker.PickSingleFileAsync();
try
{
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
playbackElement3.SetSource(stream, file.ContentType);//Play Selected
playbackElement3.Play();
}
catch (Exception ex)
{
}
然后尝试从源获取列表