1

几个小时以来,我一直在努力,阅读(一遍又一遍)我可以在 MSDN 和 Windows Dev Center 上找到的教程;没有任何效果。

我似乎无法从纯文本文件中读取数据!

是的,我已经添加了文件关联,是的,我已经在我的项目中添加了声明。

来自msdn的代码:

(function () {
    "use strict";

    WinJS.UI.Pages.define("/pages/home/home.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.


            Windows.Storage.FileIO.readTextAsync("AvailableBalance.sv").then(function (contents) {
                // Add code to process the text read from the file
                document.getElementById("AvailableBalanceText").innerText = contents;
            });
        }
    });
});

和:

<h1 id="AvailableBalanceText"></h1>

不工作。该应用程序加载正常 - 但只是不从文件中读取(或不显示数据)。

有人可以告诉我我在这里做错了什么吗?

4

1 回答 1

1

readTextAsync 不需要文件名——它需要一个 IStorageFile

您需要在某处获取对文件的引用,而不仅仅是打开它。如果它是一个用户文件,那么您需要使用documentsLibrary打开它:

Windows.Storage.KnownFolders.documentsLibrary.getFileAsync("sample.dat").done(function (yourFileHandle) {
    // you now have a file handle.
});

请注意,这是在文档库中。如果您需要访问包中的文件或其他位置(您无法访问 Metro 中的任意路径,只能访问库、应用程序的本地数据和包),您需要使用其他 API 之一。我建议查看文件访问示例

于 2012-08-29T19:34:11.753 回答