来自Windows 8中的CreateFileAsync的文件指针Javascript未定义

时间:2012-12-02 21:54:17

标签: javascript windows-8

使用Visual Studio Express在Javascript中编写Windows 8应用程序。我不确定我是否错过了链接依赖或其他东西,但我让我的游戏用户能够将他们的进度保存到文件中(至少这是个想法)。但是,当我运行以下代码时,看起来即使我可以看到那里的文件,文件指针结果是未定义的。看到我错过的任何东西?

var myStats = new Array();
myStats[0] = myTitle;
myStats[1] = currPage;
myStats[2] = currStep;
myStats[3] = currTune;
myStats[4] = musicon;
myStats[5] = gameComplete;

var save01File = null;

            Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("Asave01.dat", Windows.Storage.CreationCollisionOption.replaceExisting).done(

                function (file) {
                    save01File = file;
                },
                function (error) {
                    WinJS.log && WinJS.log(error, "sample", "error");
                });
            Windows.Storage.FileIO.writeLinesAsync(save01File, myStats).done(
                function () {
                },
            function (error) {
                WinJS.log && WinJS.log(error, "sample", "error");
                errCount++;
            });
            if (errCount == 0) {
                fileMenuTitle.text = "File Saved - Click Cancel";
                fileMenuTitle.text = fileMenuTitle.text + "\nto Return or Select Another File to Load";
                fileMenuTitle.color = "green";
            }
            else {
                fileMenuTitle.text = "There was an error saving that file.";
                fileMenuTitle.text = fileMenuTitle.text + "\nPlease Make Another Selection.";
                fileMenuTitle.color = "firebrick";
            }

1 个答案:

答案 0 :(得分:0)

您需要在此处使用承诺模式:

var myStats = new Array();
myStats[0] = myTitle;
myStats[1] = currPage;
myStats[2] = currStep;
myStats[3] = currTune;
myStats[4] = musicon;
myStats[5] = gameComplete;

var save01File = null;

Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("Asave01.dat", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
    save01File = file;
    return Windows.Storage.FileIO.writeLinesAsync(file, myStats);
}).done(function() {
    fileMenuTitle.text = "File Saved - Click Cancel";
    fileMenuTitle.text = fileMenuTitle.text + "\nto Return or Select Another File to Load";
    fileMenuTitle.color = "green";
}, function (error) {
    WinJS.log && WinJS.log(error, "sample", "error");
    fileMenuTitle.text = "There was an error saving that file.";
    fileMenuTitle.text = fileMenuTitle.text + "\nPlease Make Another Selection.";
    fileMenuTitle.color = "firebrick";
});