问题描述
我正在创建一个Windows 8 HTML5应用,并实现一个应用栏。当我访问appbar.winControl时,它始终为null。因此,例如,如果我写appbar.winControl.show(),我得到一个声明winControl为空的异常。
I am creating a Windows 8 HTML5 app, and implementing an app bar. When I access the appbar.winControl it is always null. So if for example I write appbar.winControl.show() I get an exception claiming that winControl is null.
我所做的是创建一个固定的布局应用程序。在没有做任何其他事情的情况下,我在文档正文中添加了以下标记:
What I did was create a fixed layout app. Without doing anything else, I added the following markup in the document body:
<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( )产生以下错误:
The line winControl.show() produces the following error:
0x800a138f - JavaScript运行时错误:无法获取未定义或空引用的属性show
0x800a138f - JavaScript runtime error: Unable to get property 'show' of undefined or null reference
据我所知,我已经在页面上正确实现了appbar,所以它应该可行。什么是错的?
As far as I can see I have implemented the appbar correctly on the page, so it should work. Any idea what is going wrong?
推荐答案
页面控件尚未初始化,脚本在此之前正在执行。
Page Controls are not initialized yet and script is executing before that.
此代码可以放置两个位置。或者
There is two place this code can be placed. either in
// 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();
}
}
这篇关于winjs appbar.wincontrol为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!