UI Toolbox 徽标UI Toolbox
支持

本页为译文。技术支持和视频教程主要使用英语,内容以英文页面为准。 英文版

ToolBarMenu

UIToolbox 中的 ToolBarMenu 是应用程序的菜单栏。每个 ToolBarMenuItem 是一个标题,带有一个下拉的 ContextMenuComponent。标题的子元素放在它的菜单里,在 UXML 和代码中都是如此。

展开 File 菜单的菜单栏:快捷键、子菜单、分隔线和勾选标记展开 File 菜单的菜单栏:快捷键、子菜单、分隔线和勾选标记

<ui:UXML xmlns:tb="UIToolbox" xmlns:cm="UIToolbox.Core.ContextMenu">
    <tb:ToolBarMenu name="menuBar">
        <tb:ToolBarMenuItem text="&amp;File">
            <cm:MenuItem
                text="&amp;New"
                action="new"
                shortcut="Ctrl+N" />
            <cm:MenuItem
                text="Save"
                action="save"
                shortcut="Ctrl+S"
                enabled="false" />
            <cm:MenuSeparator />
            <cm:MenuItem text="Recent">
                <cm:MenuItem
                    text="notes.txt"
                    action="recent notes.txt" />
            </cm:MenuItem>
        </tb:ToolBarMenuItem>
        <tb:ToolBarMenuItem text="View">
            <cm:MenuItem
                text="Word Wrap"
                action="wrap"
                checkable="true"
                checked="true" />
            <cm:MenuSeparator />
            <cm:MenuLabel text="Zoom" />
            <cm:MenuItem
                text="100%"
                action="zoom 100"
                radio-group="zoom"
                checked="true" />
            <cm:MenuItem
                text="200%"
                action="zoom 200"
                radio-group="zoom" />
        </tb:ToolBarMenuItem>
        <tb:ToolBarMenuItem text="Edit">
            <cm:MenuItem
                text="Delete All"
                action="delete all"
                destructive="true" />
        </tb:ToolBarMenuItem>
    </tb:ToolBarMenu>
</ui:UXML>
var bar = new ToolBarMenu();
var file = bar.AddMenu("File");
file.AddItem("New", () => NewDocument(), "Ctrl+N");
file.AddSubmenu("Recent").AddItem("notes.txt", () => Open("notes.txt"));
bar.AddMenu("View")
    .AddCheckableItem("Word Wrap", on => SetWrap(on), isChecked: true);

// Items with an action, as UXML declares them
foreach (var title in bar.Menus)
    title.Menu.ActionTriggered += (action, _) =>
        Debug.Log($"{title.Text} > {action}");
成员 说明
AddMenu(text) 追加一个标题并返回它的菜单。
Menus, GetMenu(text) 各个标题。ToolBarMenuItem 有 Text、AccessKey 和 Menu。GetMenu 比较文字时带着其中的 &。
OpenByAccessKey(key) 打开第一个具有此访问键的标题的菜单,并高亮它的第一个可用项,与 Alt 加该键的效果相同。
OverflowItem, OverflowMenus “≡”标题,以及放不下、位于它菜单中的标题。
Open(item), Close(), OpenItem 在标题下方显示它的菜单、隐藏菜单,以及菜单已打开的标题。
RunShortcut(key, modifiers) 运行第一个 Shortcut 匹配的可用项,在任何菜单或子菜单中皆可。没有匹配时返回 false。
HandleShortcuts (handle-shortcuts) 在面板中任何位置按键时运行各项的快捷键。默认开启。

ToolBarMenuDemo 场景展示了一个关卡编辑器的菜单栏:子菜单、快捷键和访问键,Grid 复选项以及缩放和画笔的单选项,在运行时添加并移入溢出菜单的标题,以及菜单栏及其菜单的各种外观。