Windows 8开发入门(十六) Windows 8的右键菜单2013-12-02 cnblogs 程兴亮在Windows 8中的控件中有TextBox等输入控件的ContextMenuOpening事件和Button等非输入控件的 RightTapped事件。本文中将讲述者两个事件的用法。这两个事件的PopupMenu是右键弹出菜单 的具体类。首先我们看具体菜单类的实例化和获取项目代码
/// <summary>/// 设置右键点击点击具体处理细节/// </summary>/// <param name="sender"></param>public async void SetRightClick(object sender){//增加菜单PopupMenu menu = new PopupMenu();menu.Commands.Add(new UICommand("复制0", null, 0));menu.Commands.Add(new UICommand("剪切1", null, 1));menu.Commands.Add(new UICommand("粘贴2", null, 2));menu.Commands.Add(new UICommandSeparator());menu.Commands.Add(new UICommand("Full Screen", null, 3));menu.Commands.Add(new UICommand("Snap Screen", null, 4));//获取选择的菜单项var cmd = await menu.ShowForSelectionAsync(GetRectPosition((FrameworkElement)sender));if (cmd != null){switch ((int)cmd.Id){case 0:tbText.Text = "选择了(" + cmd.Label + ") ,其ID为" + cmd.Id;break;case 1:tbText.Text = "选择了(" + cmd.Label + "),其ID为" + cmd.Id;break;case 2:tbText.Text = "选择了(" + cmd.Label + ") ,其ID为" + cmd.Id;break;case 3:tbText.Text = "选择了(" + cmd.Label + ") ,其ID为" + cmd.Id;break;case 4:tbText.Text = "选择了(" + cmd.Label + ") ,其ID为" + cmd.Id;break;}}else{tbText.Text = "上下文菜单";}}//获取菜单位置Rect GetRectPosition(FrameworkElement element){GeneralTransform btnform = element.TransformToVisual(null);Point point = btnform.TransformPoint(new Point());return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));}