在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)); }