我对于WinJS.Application.start()
函数的理解是,它允许WinJS排队某些正常的页面初始化事件,让您有机会首先在您的default.js
中设置其他数据文件。通过调用default.js
末尾的start()
函数,WinJS会为您排除所有排队事件(例如activated
事件)。app.onactivated取决于app.start()的位置
我想了解哪里一切都适合生命周期,所以我不清楚为什么下面的第一个例子工作,但第二个没有。我做的是更新页面标题,当我一个5秒的延迟之后调用app.start()
预期不工作:
首先,这里default.html
:
<html>
<head>
<script references...>
</head>
<body>
<h1 id="pageTitle">Page of coolness...</h1>
</body>
</html>
而这里的第一default.js
例子(按预期工作):
(function() {
var app = WinJS.Application;
app.onactivated = function() {
document.getElementById("pageTitle").innerText = "Rock it!";
};
// This code *does* fire the onactivated event:
// The page displays "Rock it!" in the page title
app.start();
})();
这里是第二default.js
例子(如预期不工作):
(function() {
var app = WinJS.Application;
app.onactivated = function() {
document.getElementById("pageTitle").innerText = "Rock it!";
};
// This code *doesn't* fire the onactivated event:
// It initially displays "Page of coolness..." in the page title.
// After 5 seconds, it adds "Gettin' there...Almost made it..."
// to the the page title, but "Rock it!" never gets displayed
WinJS.Promise.timeout(5000).then(function() {
document.getElementById("pageTitle").innerText += "Gettin' there...";
app.start();
document.getElementById("pageTitle").innerText += "Almost made it...";
});
})();
为什么在5秒后调用app.start()
导致activated
事件不火?
不是一个答案,但装载的确实火。您可以在调试器中遍历base.js,并且会看到执行的差异序列。这几乎就好像激活的事件处理程序被“连接太迟”一样,所以当它触发时,它还没有被连接,并且是空的,但是不能解释为什么或者如果这是合适的行为。 –
@Jim _“就好像被激活的事件处理程序被连接'太晚'一样,所以当它被触发时,它还没有被连接并且是空的......”_...事实证明,这正是发生了,它看起来像是按照设计的方式(尽管我不知道为什么) – RSW