因此,目前在我正在编码的Windows 8 WinJS应用程序中,我试图在应用程序启动序列中加载xml文件,而初始屏幕仍在显示,因为当主页加载,没有主页加载将失败。

这是我在default.js中的启动顺序:



(function () {
    "use strict";

    var activation = Windows.ApplicationModel.Activation;
    var app = WinJS.Application;
    var nav = WinJS.Navigation;
    var sched = WinJS.Utilities.Scheduler;
    var ui = WinJS.UI;

    app.addEventListener("activated", function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
                console.log("Newly Launched!");

                var localSettings = Windows.Storage.ApplicationData.current.localSettings;
                WinJS.Namespace.define("MyGlobals", { localSettings: localSettings });

                // APP RUN TYPE CHECK AND SEQUENCE (FIRST RUN / NOT FIRST RUN):
                if (MyGlobals.localSettings.values['firstRunCompleted']) {
                    console.log("NOT FIRST RUN!");
                    // CACHE VERSION CHECK. IF APP HAS BEEN UPDATED, INITIATE NEWLY ADDED CACHE VALUES HERE:
                } else {
                    console.log("FIRST RUN!")
                    MyGlobals.localSettings.values['firstRunCompleted'] = true;
                };


                //loadXML(); have tried many things with this. doesn't work.



            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
                var currentVolume = app.sessionState.currentVolume;
                if (currentVolume) {
                    console.log("RESTORE FROM SUSPENSION");
                    console.log(currentVolume);
                };
            }

            nav.history = app.sessionState.history || {};
            nav.history.current.initialPlaceholder = true;

            // Optimize the load of the application and while the splash screen is shown, execute high priority scheduled work.
            ui.disableAnimations();
            var p = ui.processAll().then(function () {
                return nav.navigate(nav.location || Application.navigator.home, nav.state);
            }).then(function () {
                return sched.requestDrain(sched.Priority.aboveNormal + 1);
            }).then(function () {
                ui.enableAnimations();
            });

            args.setPromise(p);
            args.setPromise(WinJS.UI.processAll().then(function completed() {

                loadSavedColour();

                // Populate Settings pane and tie commands to Settings flyouts.
                WinJS.Application.onsettings = function (e) {
                    e.detail.applicationcommands = {
                        "helpDiv": { href: "html/Help.html", title: WinJS.Resources.getString("settings_help").value },
                        "aboutDiv": { href: "html/About.html", title: WinJS.Resources.getString("settings_about").value },
                        "settingsDiv": { href: "html/Settings.html", title: WinJS.Resources.getString("settings_settings").value },
                    };
                    WinJS.UI.SettingsFlyout.populateSettings(e);
                }
                 





如您所见,在我的注释行“ loadXML()”的位置,这就是我需要loadXML()函数发生的位置。
这是我的loadXML()函数:



function loadXML() {
        Windows.ApplicationModel.Package.current.installedLocation.getFolderAsync("foldername").then(function (externalDtdFolder) {
            externalDtdFolder.getFileAsync(MyGlobals.localSettings.values['currentBook']).done(function (file) {
                Windows.Data.Xml.Dom.XmlDocument.loadFromFileAsync(file).then(function (doc) {
                    WinJS.Namespace.define("MyGlobals", {
                        xmlDoc: doc,
                    });
                })
            })
        });
    };





(loadXML是一个工作功能,在其他情况下也可以工作)

但是,问题在于,在loadXML函数完成之前,应用程序启动屏幕消失了,并加载了下一个home.html主页,这启动了随附的home.js,该home.js具有要求使用loadXML的MyGlobals.xmlDoc对象的功能。应该做的。由于MyGlobals.xmlDoc是undefined / null,因此这立即使应用程序崩溃。
我曾经通过直接在home.js页面的home.js中运行loadXML来使该应用程序正常工作,但是在这种情况下,每次导航到页面时都会重新加载XML文档,这浪费了时间和资源。因此,我正在尝试将xmldocument加载移动到应用程序启动/初始化中。
非常感谢!

最佳答案

loadXML具有异步功能,您需要进行处理。

您不应该期望loadFromFileAsync(或任何其他异步函数)在返回调用方之前已经完成。如果您的代码没有等待,您会发现在需要时不会设置MyGlobals.xmlDoc值。

我在下面将其重命名,以使其行为更准确。最大的变化是它返回一个Promise,调用方可以使用它来正确等待Xml文档被加载。如果需要,此Promise可以与其他Promise一起使用,以在多种条件下等待(或者,在其他情况下,异步工作)。

function loadXMLAsync() {
    return new WinJS.Promise(function (complete, error, progress) {
        var localSettings = MyGlobals.localSettings.values;
        var installedLocation = Windows.ApplicationModel.Package.current.installedLocation;
        installedLocation.getFolderAsync("foldername").then(function (externalDtdFolder) {
            externalDtdFolder.getFileAsync(values['currentBook']).done(function (file) {
                Windows.Data.Xml.Dom.XmlDocument.loadFromFileAsync(file).then(function (doc) {
                    complete(doc);
                });
            });
        });
    });
};


然后,在使用中:

loadXmlAsync().then(function(doc) {
    WinJS.Namespace.define("MyGlobals", {
        xmlDoc: doc,
    });
    // and any other code that should wait until this has completed
});


上面的代码不处理错误。

08-04 16:02