问题描述:

I'm creating a Windows 8 App (using HTML 5 and JavaScript) for someone and they've changed up the requirements on me as far as data storage and I could use some guidance. What I need is a data source that will persist only for the local user and not be online in a database or in the cloud. The users will be assigned a tablet with the app installed and they will enter data via forms to customize their local copy.

Here's my requirements:

-Data MUST persist through the lifetime that the app is installed on the device.

-I need to be able to query the data to some degree. I've basically got about 15-20 forms that will accept input data and then a main form that will feed off those 15-20 "sub" forms to populate drop-down and selection options.

-Size should not be an issue, it's all text data and not much of it will be entered. Can't see this going more than a couple hundred MBs over the lifetime of the app.

I've looked into XML, indexedDB (sounds good on the outside, but haven't found any kind of guarantee this will persist), and Application Data (local) which seems extremely limited in my reading capabilities.

What do you think my best bet is? Any advice would be greatly appreciated.

Thanks.

网友答案:

This should help

    (function () {
    "use strict";
    var page = WinJS.UI.Pages.define("/html/index.html", {
        ready: function (element, options) {
                   //do your things 
                }
    });

    var roamingFolder = Windows.Storage.ApplicationData.current.roamingFolder;
    var afile = "FileToStoreStuff.txt";

    function makefile() {
        roamingFolder.createFileAsync(afile, Windows.Storage.CreationCollisionOption.replaceExisting)
            .then(function (file) {
                return Windows.Storage.FileIO.writeTextAsync(file);
            })
    }

    function fileRead() {
        roamingFolder.getFileAsync(filename)
            .then(function (file) {
                return Windows.Storage.FileIO.readTextAsync(file);
            }).done(function (text) {
                    //do stuff
                );
    }

})();

This kind of assumes that your data may change. If it doesn't you can adapt a different approach, for instance replacing the roamingFolder variable with something like:

var localSettings = applicationData.localSettings;
var localFolder = applicationData.localFolder;

Take a look at the dev docs if you need to access data from within the app elsewhere.

相关阅读:
Top