-1

我有一个单词列表,我想读入字符串列表。我在使用 Metro 应用程序时遇到了一些麻烦Windows Runtime

在此处输入图像描述

通常我会使用以下代码:

'load text file in to word list
Using sr As New StreamReader(filePath)
    Do While sr.Peek <> -1
        WordList.Add(sr.ReadLine.Trim)
    Loop
End Using

我正在尝试使用The right way to Read & Write Files in WinRT 中的代码

Dim folder = Windows.ApplicationModel.Package.Current.InstalledLocation
folder = folder.GetFolderAsync("Data")
Dim file = folder.GetFileAsync("WordList.txt")
Dim readFile = Windows.Storage.FileIO.ReadTextAsync(file)

但是它在第二行被绊倒了,即使没有,我也不知道该怎么办。我已经杀死了Await关键字,因为由于某种原因它看不到方法Async上的属性GetFolder

4

1 回答 1

0

这是来自 Windows 应用商店应用开发中心的文件访问示例应用程序

Private Async Function LoadWords() As Task
    Dim fileName As String = "WordListComma.txt"
    Dim fileContent As String = ""
    Dim file As StorageFile
    Dim numBytesLoaded As UInt32
    Dim size As UInt64

    file = Await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileName)

    If file Is Nothing Then Throw New Exception(String.Format("Could not find file {0}", fileName))

    Using readStream As IRandomAccessStream = Await file.OpenAsync(FileAccessMode.Read)
        Using dataReader As New DataReader(readStream)
            size = readStream.Size
            If size <= UInt32.MaxValue Then
                numBytesLoaded = Await dataReader.LoadAsync(CType(size, UInt32))
                fileContent = dataReader.ReadString(numBytesLoaded)
            End If
        End Using
    End Using

End Function

此外,Await调用函数时必须使用关键字Async,并且只能存在于本身已用Async关键字修饰的方法内部,因此需要将其添加到 LoadWords() 签名中。

于 2013-06-28T02:52:55.713 回答