我正在创建一个 Windows 8 HTML5 应用程序,并实现一个应用程序栏。当我访问 appbar.winControl 时,它始终为空。因此,如果我编写 appbar.winControl.show() ,我会收到一个异常,声称 winControl 为 null。
我所做的是创建一个固定布局的应用程序。没有做任何其他事情,我在文档正文中添加了以下标记:
<body>
<div id="appbar" data-win-control="WinJS.UI.AppBar" aria-label="Command Bar">
<button id="btnBoardSizeUp" data-win-control="WinJS.UI.AppBarCommand" data-win-options="{label:'Larger', icon:'zoomout', section:'global', tooltip:'Increase the board size'}" />
<button id="btnBoardSizeDown" data-win-control="WinJS.UI.AppBarCommand" data-win-options="{label:'Smaller', icon:'zoomin', section:'global', tooltip:'Decrease the board size'}" />
<button id="btnAbortGame" data-win-control="WinJS.UI.AppBarCommand" data-win-options="{label:'Abort', icon:'cancel', section:'selection', tooltip:'Abort the current game'}" />
</div>
<script type="text/javascript">
var appbar = document.getElementById("appbar");
var winControl = appbar.winControl;
winControl.show();
</script>
winControl.show() 行产生以下错误:
0x800a138f - JavaScript 运行时错误:无法获取未定义或空引用的属性“show”
据我所知,我已经在页面上正确实现了应用栏,所以它应该可以工作。知道出了什么问题吗?
最佳答案
页面控件尚未初始化,脚本在此之前正在执行。
有两个地方可以放置此代码。要么在
// if the app bar is part of default.html, appbar show can be put in default.js
// default.js already have a call to WinJS.UI.processAll under activated event handler
WinJS.UI.processAll().then(function ()
{
appbar.winControl.show();
});
或
// if the appbar is part of mypage.html, need to have code like this in mypage.js
WinJS.UI.Pages.define('/pages/mypage/mypage.html',
{
ready: function onready(element, options)
{
appbar.winControl.show();
}
}
关于javascript - winjs appbar.wincontrol 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986779/