添加带有命令的工具栏 (HTML)
[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]
ToolBar 是一个简单的控件,用于解决命令扩展问题。它具有一个 action area,其中的命令可以立即使用;还具有一个 overflow area,其中的命令处于隐藏状态,但可以根据最终用户发出的请求进行显示。该控件通过在空间受限时,允许命令以动态方式从主要区域移动到辅助区域,从而支持自适应行为。 ToolBar 不限于某个应用中的单个位置,而可以位于如 Splitview、Flyout 等多个位置中以及 canvas 上。
注意 可以在尝试 WinJS 网站上查看以下编码方案的详细信息。
创建带有以声明方式添加的命令的工具栏
可以以声明方式将命令添加到工具栏。在此方案中,工具栏具有四个主要命令和两个辅助命令。
<div class="basicToolbar" data-win-control="WinJS.UI.ToolBar">
<!-- Primary commands -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdEdit',
label:'edit',
section:'primary',
type:'button',
icon: 'edit',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdDelete',
label:'delete',
section:'primary',
type:'button',
icon: 'delete',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdFavorite',
label:'favorite',
section:'primary',
type:'toggle',
icon: 'favorite',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdOpenWith',
label:'open with',
section:'primary',
type:'button',
icon: 'openfile',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdDownload',
label:'download',
section:'primary',
type:'button',
icon: 'download',
onclick: Sample.outputCommand}"></button>
<!-- Secondary command -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdSettings',
label:'settings',
section:'secondary',
type:'button',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdShare',
label:'share',
section:'secondary',
type:'button',
onclick: Sample.outputCommand}"></button>
</div>
<div class="status"></div>
WinJS.Namespace.define("Sample", {
outputCommand: WinJS.UI.eventHandler(function (ev) {
var status = document.querySelector(".status");
var command = ev.currentTarget;
if (command.winControl) {
var label = command.winControl.label || command.winControl.icon || "button";
var section = command.winControl.section || "";
var msg = section + " command " + label + " was pressed";
status.textContent = msg;
}
})
});
WinJS.UI.processAll();
指定命令的分组和退出顺序
开发人员可以针对不遵循可见的从右到左顺序的溢出区域指定命令的分组和退出顺序。这可用于屏幕空间受限的情况。控件按从最高值到最低值的顺序退出。默认情况下,这些命令按从右到左的顺序退出。但优先级的默认值是未定义的。
<div class="groupingToolbar" data-win-control="WinJS.UI.ToolBar">
<!-- Primary commands -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdEdit',
label:'edit',
section:'primary',
type:'button',
icon: 'edit',
priority:2,
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdDelete',
label:'delete',
section:'primary',
type:'button',
icon: 'delete',
priority:2,
onclick: Sample.outputCommand}"></button>
<hr data-win-control="WinJS.UI.Command" data-win-options="{type:'separator'}" />
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdFavorite',
label:'favorite',
section:'primary',
type:'toggle',
icon: 'favorite',
priority:3,
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdOpenWith',
label:'open with',
section:'primary',
type:'button',
icon: 'openfile',
priority:3,
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdDownload',
label:'download',
section:'primary',
type:'button',
icon: 'download',
priority:3,
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdPin',
label:'pin',
section:'primary',
type:'button',
icon: 'pin',
priority:3,
onclick: Sample.outputCommand}"></button>
<hr data-win-control="WinJS.UI.Command" data-win-options="{type:'separator'}" />
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdZoom',
label:'zoom',
section:'primary',
type:'button',
icon: 'zoomin',
priority:1,
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdFullscreen',
label:'full screen',
section:'primary',
type:'button',
icon: 'fullscreen',
priority:1,
onclick: Sample.outputCommand}"></button>
<!-- Secondary command -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdSettings',
label:'settings',
section:'secondary',
type:'button',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdShare',
label:'share',
section:'secondary',
type:'button',
onclick: Sample.outputCommand}"></button>
</div>
<div class="status"></div>
WinJS.Namespace.define("Sample", {
outputCommand: WinJS.UI.eventHandler(function (ev) {
var status = document.querySelector(".status");
var command = ev.currentTarget;
if (command.winControl) {
var label = command.winControl.label || command.winControl.icon || "button";
var section = command.winControl.section || "";
var priority = command.winControl.priority;
var msg = section + " command " + label + " with priority " + priority + " was pressed";
status.textContent = msg;
}
})
});
WinJS.UI.processAll();
同时显示多个工具栏
开发人员可以创建多个工具栏,并同时显示它们。
<div class="sampleToolBar" data-win-control="WinJS.UI.ToolBar">
<!-- Primary commands -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdEdit',
label:'edit',
section:'primary',
type:'button',
icon: 'edit',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdFavorite',
label:'favorite',
section:'primary',
type:'toggle',
icon: 'favorite',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdDelete',
label:'delete',
section:'primary',
type:'button',
icon: 'delete',
onclick: Sample.outputCommand}"></button>
<!-- Secondary commands -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdSettings',
label:'settings',
section:'secondary',
type:'button',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdShare',
label:'share',
section:'secondary',
type:'button',
onclick: Sample.outputCommand}"></button>
</div>
<div class="sampleToolBar2" data-win-control="WinJS.UI.ToolBar">
<!-- Primary commands -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdBold',
section:'primary',
type:'toggle',
icon: 'bold',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdItalic',
section:'primary',
type:'toggle',
icon: 'italic',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdUnderline',
section:'primary',
type:'toggle',
icon: 'underline',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdEmoticon',
section:'primary',
type:'button',
icon: 'emoji',
onclick: Sample.outputCommand}"></button>
<!-- Secondary commands -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdSettings',
label:'settings',
section:'secondary',
type:'button',
onclick: Sample.outputCommand}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdShare',
label:'share',
section:'secondary',
type:'button',
onclick: Sample.outputCommand}"></button>
</div>
<div class="status"></div>
WinJS.Namespace.define("Sample", {
outputCommand: WinJS.UI.eventHandler(function (ev) {
var status = document.querySelector(".status");
var command = ev.currentTarget;
if (command.winControl) {
var label = command.winControl.label || command.winControl.icon || "button";
var section = command.winControl.section || "";
var msg = section + " command " + label + " was pressed";
status.textContent = msg;
}
})
});
WinJS.UI.processAll();
创建带有使用 WinJS.Binding.List 添加的命令的工具栏
WinJS.Binding.List 可用于通过带有命令的工具栏的 data 属性,填充该工具栏。
<div class="imperativeToolBar" data-win-control="WinJS.UI.ToolBar"
data-win-options="{data: Sample.commandList}"></div>
<div class="status"></div>
WinJS.Namespace.define("Sample", {
commandList: null,
outputCommand: WinJS.UI.eventHandler(function (ev) {
var status = document.querySelector(".status");
var command = ev.currentTarget;
if (command.winControl) {
var label = command.winControl.label || command.winControl.icon || "button";
var section = command.winControl.section || "";
var msg = section + " command " + label + " was pressed";
status.textContent = msg;
}
})
});
var dataArray = [
new WinJS.UI.Command(null,
{ id: 'cmdEdit', label: 'edit', section: 'primary', type: 'button', icon: 'edit', onclick: Sample.outputCommand }),
new WinJS.UI.Command(null,
{ id: 'cmdDelete', label: 'delete', section: 'primary', type: 'button', icon: 'delete', onclick: Sample.outputCommand }),
new WinJS.UI.Command(null,
{ id: 'cmdFavorite', label: 'favorite', section: 'primary', type: 'toggle', icon: 'favorite',
onclick: Sample.outputCommand }),
new WinJS.UI.Command(null,
{ id: 'cmdOpenWith', label: 'open with', section: 'primary', type: 'button', icon: 'openfile',
onclick: Sample.outputCommand }),
new WinJS.UI.Command(null,
{ id: 'cmdDownload', label: 'download', section: 'primary', type: 'button', icon: 'download',
onclick: Sample.outputCommand }),
new WinJS.UI.Command(null,
{ id: 'cmdPin', label: 'pin', section: 'primary', type: 'button', icon: 'pin', onclick: Sample.outputCommand }),
new WinJS.UI.Command(null,
{ id: 'cmdZoom', label: 'zoom', section: 'primary', type: 'button', icon: 'zoomin', onclick: Sample.outputCommand }),
new WinJS.UI.Command(null,
{ id: 'cmdFullscreen', label: 'full screen', section: 'primary', type: 'button', icon: 'fullscreen',
onclick: Sample.outputCommand })
];
Sample.commandList = new WinJS.Binding.List(dataArray);
WinJS.UI.processAll();
自定义工具栏
工具栏的默认颜色由 ui-dark.css 或 ui-light.css 样式表设置,具体取决于加载的是哪个样式表。 可以通过重写相应的 CSS 规则来自定义工具栏的颜色。在此示例中,将工具栏的背景色设置为透明,将工具栏的溢出区域的背景色自定义为与它后面的背景图像相匹配。
<div class="image">
<img src="/pages/toolbar/images/Sunset.jpg" alt="" />
<div data-win-control="WinJS.UI.ToolBar">
<!-- Primary commands -->
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdEdit',
label:'edit',
section:'primary',
type:'button',
icon: 'edit'}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdFavorite',
label:'favorite',
section:'primary',
type:'toggle',
icon: 'favorite'}"></button>
<button data-win-control="WinJS.UI.Command" data-win-options="{
id:'cmdDelete',
label:'delete',
section:'primary',
type:'button',
icon: 'delete'}"></button>
</div>
</div>
/* Add your CSS here */
body {
background-color: rgb(112,112,112);
}
#content {
text-align: center;
overflow: hidden;
}
.image {
position: relative;
margin: auto;
margin-top: 50px;
margin-bottom:50px;
}
img {
max-width: 100%;
}
.win-toolbar-actionarea {
background: transparent;
}
.win-toolbar-overflowarea {
background-color: rgb(74, 61, 78);
border: 0;
}
WinJS.UI.processAll();
备注
可以在尝试 WinJS 网站上查看以下编码示例和其他编码示例的详细信息。使用代码并立即看到结果。