现在的位置: 首页 > 综合 > 正文

windows8 上下文菜单

2013年03月18日 ⁄ 综合 ⁄ 共 1872字 ⁄ 字号 评论关闭
上下文菜单示例
这个示例展示了如何创建一个上下文菜单和如何替换默认的上下文菜单文本。这个示例使用Windows.UI。 弹出API,包括PopupMenu和oncontextmenu事件。
这个示例演示了这两个任务:
创建一个上下文菜单显示为一个文件
替换默认命令在上下文菜单中显示的文本
了解选择命令和设计一个上下文菜单,看到指导方针和检查表上下文菜单。
额外的api对于此示例包括:
•UICommand类
•UICommandSeparator类
async void Scenario2TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            // Create a menu and add commands specifying an id value for each instead of a delegate.
            var menu = new PopupMenu();
            menu.Commands.Add(new UICommand("Copy"null1));
            menu.Commands.Add(new UICommandSeparator());
            menu.Commands.Add(new UICommand("Highlight"null2));
            menu.Commands.Add(new UICommand("Look up on dictionary"null3));

            // We don't want to obscure content, so pass in a rectangle representing the sender of the context menu event.
            
// We registered command callbacks; no need to await and handle context menu completion
            var chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
            if (chosenCommand != null)
            {
                switch ((int)chosenCommand.Id)
                {
                    case 1:
                        Output2Text.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected";
                        break;

                    case 2:
                        Output2Text.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected";
                        break;

                    case 3:
                        Output2Text.Text = "'" + chosenCommand.Label + "'(" + chosenCommand.Id.ToString() + ") selected";
                        break;
                }
            }
            else
            {
                Output2Text.Text = "Context menu dismissed";
            }

        } 

 完整示例:/Files/risk/windows8/上下文菜单sample.rar

抱歉!评论已关闭.