第二节 Managing application data
http://msdn.microsoft.com/en-us/library/windows/apps/br229565.aspx
Local application data
给我的感觉这个类似于 cooikes.下面看用法;
下面页面定义三个div用于显示 三种本地数据
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8"> <title>navigationApp</title> <!-- WinJS references --> <link href="//Microsoft.WinJS.0.6/css/ui-dark.css" rel="stylesheet"> <script src="//Microsoft.WinJS.0.6/js/base.js"></script> <script src="//Microsoft.WinJS.0.6/js/ui.js"></script> <!-- navigationApp references --> <link href="/css/default.css" rel="stylesheet"> <script type="text/javascript" src="js/script.js"></script> </head> <body> <div id="ttt" style="height: 100px; width: 100px; border: 1px solid red; background: white; color: #000;"></div> <div id="Div1" style="height: 100px; width: 100px; border: 1px solid red; background: white; color: #000;"></div> <div id="Div2" style="height: 100px; width: 100px; border: 1px solid red; background: white; color: #000;"></div> </body> </html>
这个页面应用了 script.js文件,下面是这个js文件的代码
// For an introduction to the Blank template, see the following documentation: // http://go.microsoft.com/fwlink/?LinkId=232509 (function () { "use strict"; var app = WinJS.Application; app.onactivated = function (eventObject) { if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) { if (eventObject.detail.previousExecutionState !== Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) { // TODO: This application has been newly launched. Initialize // your application here. } else { // TODO: This application has been reactivated from suspension. // Restore application state here. } WinJS.UI.processAll().then(function () { var applicationData = Windows.Storage.ApplicationData.current; var localSettings = applicationData.localSettings; var localFolder = applicationData.localFolder; // Simple setting localSettings.values["na"] = "asd"; // Composite setting var composite = Windows.Storage.ApplicationDataCompositeValue(); composite["name"] = "gao"; composite["age"] = 12; localSettings.values["compositeSettings"] = composite; // Setting in a container var containers = localSettings.createContainer("newContainers", Windows.Storage.ApplicationDataCreateDisposition.always); if (localSettings.containers.hasKey("newContainers")) { localSettings.containers.lookup("newContainers").values["newSetting"] = "news"; } //读取数据 //Simple setting ttt.innerText = localSettings.values["na"]; // Composite setting var composite = localSettings.values["compositeSettings"]; if (composite) { Div1.innerText = composite["name"]; } //Setting in a container var hasContainer = localSettings.containers.hasKey("newContainers"); if (hasContainer) { Div2.innerText = localSettings.containers.lookup("newContainers").values["newSetting"]; }
//删除数据 //Simple setting localSettings.values.remove("na"); // Composite setting localSettings.deleteContainer("newContainers");}); } }; app.oncheckpoint = function (eventObject) { }; app.start(); })();
Write data to a file
把当前时间保存到临时文件,保存后读入到div中
html代码:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta charset="utf-8"> <title>navigationApp</title> <!-- WinJS references --> <link href="//Microsoft.WinJS.0.6/css/ui-dark.css" rel="stylesheet"> <script src="//Microsoft.WinJS.0.6/js/base.js"></script> <script src="//Microsoft.WinJS.0.6/js/ui.js"></script> <!-- navigationApp references --> <link href="/css/default.css" rel="stylesheet"> <script type="text/javascript" src="js/script.js"></script> </head> <body> <div id="ttt" style="height: 100px; width: 100px; border: 1px solid red; background: white; color: #000;"></div> </body> </html>
script.js代码:
// For an introduction to the Blank template, see the following documentation: // http://go.microsoft.com/fwlink/?LinkId=232509 (function () { "use strict"; var app = WinJS.Application; function writeTimestamp() { var applicationData = Windows.Storage.ApplicationData.current; var localFolder = applicationData.localFolder; localFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting) .then(function (sampleFile) { //写入 var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime"); var timestamp = formatter.format(new Date());//获取当前时间,用于写入到文件 return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp); // 写入到文件 }).done(function () { }); } function readTimestamp() { var applicationData = Windows.Storage.ApplicationData.current; var localFolder = applicationData.localFolder; localFolder.getFileAsync("dataFile.txt") .then(function (sampleFile) { return Windows.Storage.FileIO.readTextAsync(sampleFile); }).done(function (timestamp) { ttt.innerText = timestamp; }, function () { // Timestamp not found }); } app.onactivated = function (eventObject) { if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) { if (eventObject.detail.previousExecutionState !== Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) { // TODO: This application has been newly launched. Initialize // your application here. } else { // TODO: This application has been reactivated from suspension. // Restore application state here. } WinJS.UI.processAll().then(function () { writeTimestamp(); readTimestamp(); }); } }; app.oncheckpoint = function (eventObject) { }; app.start(); })(); 程序运行,效果如下:
下面是 新建的临时文件:
Roaming application data
原文地址:http://msdn.microsoft.com/en-us/library/windows/apps/hh465123.aspx
var applicationData = Windows.Storage.ApplicationData.current; //给漫游数据添加数据改变处理程序 applicationData.addEventListener("datachanged", dataChangeHandler); function dataChangeHandler(eventArgs) { //更新数据 }
Temporary application data
http://msdn.microsoft.com/en-us/library/windows/apps/hh465130.aspx
js文件:
// For an introduction to the Blank template, see the following documentation: // http://go.microsoft.com/fwlink/?LinkId=232509 (function () { "use strict"; var app = WinJS.Application; //写入数据 function writeTimestamp() { var applicationData = Windows.Storage.ApplicationData.current; var temporaryFolder = applicationData.temporaryFolder; temporaryFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting) .then(function (sampleFile) { var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime"); var timestamp = formatter.format(new Date()); return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp); }).done(function () { }); } //读取数据 function readTimestamp() { var applicationData = Windows.Storage.ApplicationData.current; var temporaryFolder = applicationData.temporaryFolder; temporaryFolder.getFileAsync("dataFile.txt") .then(function (sampleFile) { return Windows.Storage.FileIO.readTextAsync(sampleFile); }).done(function (timestamp) { ttt.innerText = timestamp; }, function () { // Timestamp not found }); } app.onactivated = function (eventObject) { if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) { if (eventObject.detail.previousExecutionState !== Windows.ApplicationModel.Activation.ApplicationExecutionState.terminated) { // TODO: This application has been newly launched. Initialize // your application here. } else { // TODO: This application has been reactivated from suspension. // Restore application state here. } WinJS.UI.processAll().then(function () { writeTimestamp(); readTimestamp(); }); } }; app.oncheckpoint = function (eventObject) { }; app.start(); })();