javascript - winjs appbar.wincontrol 为空

标签 javascript html windows-8 windows-runtime appbar

我正在创建一个 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/

相关文章:

javascript - 单击按钮打开一个新的 InPrivate 浏览器窗口

javascript - Trumboqyg 所见即所得编辑器不适用于 Jquery.Validate 插件, "Uncaught TypeError: Cannot read property ' nodeName' of undefined"

用户输入电子邮件时用于电子邮件验证的 JavaScript 代码不起作用

html - Angular 2 选择 <li> 并获取文本

jquery - Drupal 8 为什么我的主题找不到 style.css?

javascript - 试图重写 playAudio 但不起作用

html - 菜单对齐居中和 float 问题

javascript - 向范围类型输入控件的最小值和最大值添加标签?

c# - Windows 8 Metro 应用程序 - 渲染 PNG

javascript - XAML 可以在 WinRT 中与 JavaScript 一起使用吗?