Windows store app Settings 的 应用 ( viewmodel + windows.storage)

时间:2021-01-17 21:16:03

1.在首页 加入 一个元素(加下滑线的)。此元素绑定了两个属性

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>App1</title> <!-- WinJS references -->
<link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
<script src="//Microsoft.WinJS.1.0/js/base.js"></script>
<script src="//Microsoft.WinJS.1.0/js/ui.js"></script> <!-- App1 references -->
<link href="/css/default.css" rel="stylesheet" />
<script src="/js/default.js"></script>
<script src="/js/util.js" type="text/javascript"> </script>
</head>
<body>
<div id="aa" data-win-bind="style.color:color;innerText:text" style="height: 40px; width: 100px"></div>
</body>
</html>

2.定义viewmodel, 并监听 viewmodel的属性变化。这样就可以在属性变化的时候,在更新UI的同时,去做想做的事。比如:更改本地存储。首页js 如下:

(function () {
"use strict"; WinJS.Binding.optimizeBindingReferences = true; var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
   WinJS.Application.onsettings = function (e) { //添加设置的选项
e.detail.applicationcommands = {// href: 是设置相关信息使用的页面 | title: 就是 设置显示的文字 | color : 对应 settings.html 内 拥有 data-win-control="WinJS.UI.SettingsFlyout" 属性的元素的id
"color": { href: "pages/settings.html", title: "更改字体颜色&内容" }
}; //把 自定义的设置选项 添加到 设置窗口
WinJS.UI.SettingsFlyout.populateSettings(e);
}; app.onactivated = function (args) {
if (args.detail.kind == activation.ActivationKind.launch) { //定义一个 ViewModel
WinJS.Namespace.define("ViewModel", {
Settings: WinJS.Binding.as({
color: "red",
text: 'aaaasdasdasdsa'
})
}); //需要观察的属性
var settings = ['color', 'text']; //给 viewmodel 绑定 属性改变事件
AddisonUitl.BindViewModelEvent(ViewModel.Settings, settings); WinJS.UI.processAll().then(function () { //绑定数据
WinJS.Binding.processAll(document.getElementById('aa'), ViewModel.Settings);
})
}
} app.start();
})();

3.因为在 添加自定义设置 选项的时候,需要一个设置页面,下面是 settings.html

<!DOCTYPE HTML>
<html>
<head>
<title></title>
<style>
#colorsDiv #backbutton {
color: #ffd800;
border-color: #ffd800;
} #colorsDiv div.win-header {
color: white;
font-weight: bolder;
background-color: #0094ff;
}
</style>
<script>
WinJS.UI.Pages.define("pages/settings.html", {
ready: function () { document.getElementById("backbutton").addEventListener("click", function () {
WinJS.UI.SettingsFlyout.show();
var bgColor = document.getElementById('bg').value,
text = document.getElementById('text2').value; //更改viewmodel的属性值,触发 viewmodel bind的事件,从而去更改 本地存储的值。
ViewModel.Settings.color = bgColor;
ViewModel.Settings.text = text;
}); }
});
</script>
</head>
<body>
<div id="color" data-win-control="WinJS.UI.SettingsFlyout">
<div class="win-header">
<button id="backbutton" class="win-backbutton"></button>
<div class="win-label">Colors</div>
</div>
<div class="win-content"> <h1>背景颜色</h1>
<input id="bg" /> <h1>更改内容</h1>
<input id="text2">
</div>
</div>
</body>
</html>

4.下面是 util.js 的代码:

WinJS.Namespace.define('AddisonUitl', {

    /// <summary> </summary>
/// <param name="name" type=""> property of ViewModel </param>
/// <param name="eventType" type=""> the key in param name </param>
BindViewModelEvent: function (name, eventType) {
var that = this;
eventType.forEach(function (item, index) { //载入 用户设置 信息
var valArray = Windows.Storage.ApplicationData.current.localSettings.values;
if (valArray[item] != undefined) {
name[item] = valArray[item];
} //绑定属性改变事件
name.bind(item, function (newVal, oldVal) { if (oldVal != null) {
that.storeSetting(item, newVal);
// that.updateViewModel(name, item, newVal);
}
})
})
}, storeSetting: function (key, value) {
var store = Windows.Storage;
store.ApplicationData.current.localSettings.values[key] = value;
}, updateViewModel: function (name, key, value) {
name[key] = value;
}
})