积极答复者
通过FolderPicker选择文件夹并通过FileInformationFactory查询文件,在对FileInformationFactory的GetFilesAsync()的结果遍历获取FileInformation的MusicProperties时异常,提示System.NullReferenceException: Object reference not set to an instance of an object.

问题
-
var folderPicker = new Windows.Storage.Pickers.FolderPicker();
folderPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;
folderPicker.FileTypeFilter.Add("*");
Windows.Storage.StorageFolder musicFolder = await folderPicker.PickSingleFolderAsync();
if (musicFolder != null)
{
var queryOptions = new Windows.Storage.Search.QueryOptions()
{
FolderDepth = Windows.Storage.Search.FolderDepth.Deep,
IndexerOption = Windows.Storage.Search.IndexerOption.UseIndexerWhenAvailable
};
var musicType = new List<string>() { ".mp3", ".wma", ".wav", ".flac" };
foreach (var type in musicType)
{
queryOptions.FileTypeFilter.Add(type);
}
var musicQuery = musicFolder.CreateFileQueryWithOptions(queryOptions);
var fileInformationFactory = new FileInformationFactory(musicQuery, ThumbnailMode.MusicView);
var files = await fileInformationFactory.GetFilesAsync();
foreach(var file in files)
{
var title = file.MusicProperties.Title; //异常
}
}
全部回复
-
你好
我运行了你的代码,没有异常提示。
如果是 var title = file.MusicProperties.Title; 这行代码出错,可以在可以断点,看看 file 都有哪些信息,贴出来一起看一下。
另外,你的 UWP App 的目标版本和最低版本分别是多少?我的测试环境分别是 15063 和 10586.
希望我的答案能帮助更多的人。
博客地址: http://www.cnblogs.com/shaomeng