没有GetFilesAsync的定义

时间:2014-11-13 17:58:04

标签: c# asynchronous storagefile storagefolder

每当我尝试这段代码时:

  foreach (StorageFile filesAsync in await folderFromPathAsync.GetFolderAsync(_selectedPlayList).GetFilesAsync())//Error here
        {
            this._fileNames.Add(filesAsync.Name.Substring(0, filesAsync.Name.LastIndexOf('.')));
            this.FilesListBox.Items.Add(filesAsync.Name.Substring(0, filesAsync.Name.LastIndexOf('.')));
            this._pathNames.Add(filesAsync.Path);
        }

GetFilesAsync()定义并不存在,原因有些奇怪。这是我的错吗? 我的完整错误消息:

  

'Windows.Foundation.IAsyncOperation<Windows.Storage.StorageFolder>'不包含'GetFilesAsync'的定义,并且没有可以找到接受“Windows.Foundation.IAsyncOperation”类型的第一个参数的扩展方法“GetFilesAsync”(您是否缺少using指令或程序集) refrence?)

请帮帮我

1 个答案:

答案 0 :(得分:1)

您正试图致电GetFilesAsync()调用GetFolderAsync()的结果 - 而您应该等待GetFolderAsync()的结果然后调用GetFilesAsync()的结果等待。 GetFolderAsync()的返回值不是文件夹 - 它是获取文件夹的异步操作。 (这是IAsyncOperation<StorageFolder>,而不是StorageFolder。)

例如:

var folder = await folderFromPathAsync.GetFolderAsync(_selectedPlayList);
foreach (var file in await folder.GetFilesAsync())
{
    ...
}