(function () {
"use strict";
// 标题,在/sample-utils/sample-utils.js被使用。
var sampleTitle = "File picker JS sample";
// 具体若干种"文件选择"操作,每一种操作都通过一个html/js/css集合来实现
var scenarios = [
{ url: "/html/scenario1.html", title: "Pick a single photo" },
{ url: "/html/scenario2.html", title: "Pick multiple files" },
{ url: "/html/scenario3.html", title: "Pick a folder" },
{ url: "/html/scenario4.html", title: "Save a file" },
{ url: "/html/scenario5.html", title: "Open a cached file" },
{ url: "/html/scenario6.html", title: "Update a cached file" }
];
// 程序被激活时调用
function activated(eventObject) {
if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
// Use setPromise to indicate to the system that the splash screen must not be torn down
// until after processAll and navigate complete asynchronously.
eventObject.setPromise(WinJS.UI.processAll().then(function () {
// Navigate to either the first scenario or to the last running scenario
// before suspension or termination.
// 恢复上次的url,若不存在,则使用第一个
var url = WinJS.Application.sessionState.lastUrl || scenarios[0].url;
// 浏览该url,这将触发navigated事件,见下面分析
return WinJS.Navigation.navigate(url);
}));
}
}
// WinJS.Navigation的 navigated事件处理函数
WinJS.Navigation.addEventListener("navigated", function (eventObject) {
var url = eventObject.detail.location;
// 若id=contentHost的元素存在,则清空它
// 该元素在default.html中定义
var host = document.getElementById("contentHost");
// Call unload method on current scenario, if there is one
host.winControl && host.winControl.unload && host.winControl.unload();
WinJS.Utilities.empty(host);
eventObject.detail.setPromise(
// 将改元素重新渲染为新的url(html文件)
WinJS.UI.Pages.render(url, host, eventObject.detail.state).then(function () {
// 同时重置最近的url记录
WinJS.Application.sessionState.lastUrl = url;
}
));
});
// 将“标题“和”操作“数组放入一个特定的名字空间中,其他js必须通过这个名字空间访问
WinJS.Namespace.define("SdkSample", {
sampleTitle: sampleTitle,
scenarios: scenarios
});
// 注册激活事件
WinJS.Application.addEventListener("activated", activated, false);
// 开始运行
WinJS.Application.start();
})();