Add AppBarCommand to AppBar in WinJS
NickName:user1325179 Ask DateTime:2015-08-02T06:14:43

Add AppBarCommand to AppBar in WinJS

I need to add an AppBarCommand to an AppBar programmatically. This used to work on Windows 8.1, but it's not working on Windows 10. I receive no errors, but when I execute the below code, my application bar is empty. Does anyone know if adding AppBarCommands to the AppBar dynamically from WinJS is possible anymore? If so, how can it be done? Thanks.

Although my problem was occurring within a larger existing application, I reproduced the problem from the "Blank" WinJS template.

default.js:

(function () {
    "use strict";

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                var appBarDiv = document.createElement("div");
                document.body.appendChild(appBarDiv);
                var appBar = new WinJS.UI.AppBar(appBarDiv, {});
                var command = new WinJS.UI.AppBarCommand(null, { id: "commandID", type:"button", label: "Label", section: "secondary" });
                appBar.commands = [command];
            }
            args.setPromise(WinJS.UI.processAll());
        }
    };
    app.start();
})();

default.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>App1</title>

    <!-- WinJS references -->
    <link href="WinJS/css/ui-dark.css" rel="stylesheet" />
    <script src="WinJS/js/base.js"></script>
    <script src="WinJS/js/ui.js"></script>

    <!-- App1 references -->
    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>
</head>
<body class="win-type-body">
    <p>Content goes here</p>
</body>
</html>

Copyright Notice:Content Author:「user1325179」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31766724/add-appbarcommand-to-appbar-in-winjs

Answers
Alexander Korzhykov 2015-08-24T10:02:21

As a workaround you can create all buttons at the start but then make them hidden or visible dynamically.",


More about “Add AppBarCommand to AppBar in WinJS” related questions

Add AppBarCommand to AppBar in WinJS

I need to add an AppBarCommand to an AppBar programmatically. This used to work on Windows 8.1, but it's not working on Windows 10. I receive no errors, but when I execute the below code, my

Show Detail

winjs appbar.wincontrol is null

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

Show Detail

How do I set the appBar at the top of the screen in windows 8.1 universal app WinJS

I have tried to set the app bar at the top of the screen by setting var appBar = document.getElementById("commandsAppBar").winControl; appBar.hide(); appBar.disabled = true; appBar.layout = "custo...

Show Detail

Windows Phone 8.1 WinJS AppBar disappears if I leave the app (suspended) and go back inside

Everything works perfectly. The appbar is displayed only in the home, but if I exit the App without closing and again opening it, it disappears the appbar In home.js WinJS.UI.Pages.define("/page...

Show Detail

Howto: Programmatically add AppBarCommand to Windows Phone 8.1 app

I have a Windows Store app that uses the following code to add AppBarCommands and that code does not work on Windows Phone 8.1 function showBars() { var appBar = document.createElement('div');...

Show Detail

How to set AppBarCommand align to left in WinJS 4?

some tutorials said, set section to "selection' then align will be left to right. but it not works for me. how to set AppBarCommand align to left in WinJS 4?

Show Detail

How can I databind the label of a WinJS.UI.AppBarCommand?

With Visual Studio 2012 I created a JavaScript Blank App, added a global variable in js/default.js: var mylabel = "my label"; and I called WinJS.Binding.processAll(); at the end of app.onactiv...

Show Detail

Remote URL (http) as icon for AppBarCommand in WinJS Universal App

First off, this is a Universal App and this specifically is happening when it is run on a Windows Phone. I am creating an AppBarCommand in my app via JavaScript. Here is the code: var commandEle...

Show Detail

Windows Phone 8.1 WinJS Appbar disabled property not working

I'm trying to hide (disable) the appBar so it doesn't show on up a page in my app. However the disabled property doesn't seem to be working, or my syntax is off. I've tried using it like: &lt;scri...

Show Detail

How to replace a button in appbar on windows 8 app using HTML/Javascript?

I am new to develop Windows 8 app using HTML/Javascript. I want to create/remove buttons in appbar dynamically. For example, I have the following appbar div in html. &lt;div id="appbar" data-win-

Show Detail