改性剂异步不适用于这个项目 [英] The modifier async is not valid for this item
问题描述
这不会出现被数百其它问题,具有相同的错误的副本。我已经看过所有这些,都发现他们是无关的。的
我想提出一个小纸条的应用程序,我试图从目录中读取文件。继MSDN的例子,我有以下的code,但它给我的错误:
I am making a little note app and am trying to read files from a directory. Following the MSDN example, I have the following code, but it is giving me an Error of:
错误1修改异步并不适用于该 项目C:\用户\ Jase \文档\ Visual Studio中 2012 \项目\ AppNameHere \ AppNameHere \的DataModel \ AppNameHereDataSource.cs 192 9 AppNameHere
Error 1 The modifier 'async' is not valid for this item C:\Users\Jase\documents\visual studio 2012\Projects\AppNameHere\AppNameHere\DataModel\AppNameHereDataSource.cs 192 9 AppNameHere
在code我拥有的是:
The code I have is:
async public NotesDataSource()
{
StorageFolder documentsFolder = KnownFolders.DocumentsLibrary;
StringBuilder outputText = new StringBuilder();
IReadOnlyList<StorageFile> fileList =
await documentsFolder.GetFilesAsync();
outputText.AppendLine("Files:");
foreach (StorageFile file in fileList)
{
if (file.FileType == "txt")
{
outputText.Append(file.Name + "\n");
}
}
// lots of irrelevant code removed.
}
我不明白是怎么回事。我也跟着一切一个T。可有人请帮助?
I don't understand what's going on here. I followed everything to a "T". Can somebody please help?
感谢您!
推荐答案
您的方法签名不正确。看看它:
Your method signature is incorrect. Look at it:
async public NotesDataSource()
首先,异步
有来的在的访问修饰符IIRC。
Firstly, async
has to come after the access modifier IIRC.
其次,无论你想创建一个异步的构造方法(你不能这样做),或者你想要写一个方法没有返回类型(这同样是无效的)。
Secondly, either you're trying to create an async constructor (which you can't do) or you're trying to write a method without a return type (which is equally invalid).
试试这个:
public async Task NotesDataSource()
这就是,如果你的意思是这是一个方法。如果你想的有效的创建一个异步的构造函数(或接近它),你必须使用一个异步静态方法:
That's if you meant it to be a method. If you want to effectively create an async constructor (or something close to it) you'd have to use an async static method:
public static async Task<NotesDataSource> CreateInstance()
{
// Do async stuff here which fetches all the necessary data...
return new NotesDataSource(...);
}
这篇关于改性剂异步不适用于这个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!