在程序中,當右鍵單擊某個對象時,會有小小的彈出菜單命令。如果你使用過Windows 8,你可能已經遇見到過上下文菜單了。經常在一些不可以選擇的對象上右鍵單擊,或者在text文本上進行操作時,會出現(xiàn)上下文菜單。什么時候使用上下文菜單,微軟提供了非常詳細的指導,下面將介紹如何實現(xiàn)上下文菜單。
1.確定要顯示上下文菜單的位置
當我們創(chuàng)建彈出菜單時,首先需要確定出被點擊element所在的位置,然后將位置傳遞給彈出菜單控件。下面的方法是確定element的位置:
privateRect GetPoint(TextBox box)/p>
p> { /p>
p> Rect temp = box.GetRectFromCharacterIndex(box.SelectionStart, false);/p>
p> GeneralTransform transform = box.TransformToVisual(null);/p>
p> Point point = transform.TransformPoint(new Point());/p>
p> point.X = point.X + temp.X;/p>
p> point.Y = point.Y + temp.Y;/p>
p> return new Rect(point, new Size(temp.Width, temp.Height));/p>
p> }
2.創(chuàng)建上下文菜單的選項
1)為TextBox添加上下文菜單事件
protected override void OnNavigatedTo(NavigationEventArgs e)/p>
p> {/p>
p> InputBox.ContextMenuOpening += InputBox_ContextMenuOpening; //InputBox是UI層的TextBox控件/p>
p> }
2)當離開這個頁面時移除上下文菜單事件
protected override void OnNavigatedFrom(NavigationEventArgs e)/p>
p> {/p>
p> InputBox.ContextMenuOpening -= InputBox_ContextMenuOpening;/p>
p> }
3)創(chuàng)建了一個PopupMenu菜單,并添加了一個command,然后調用ShowForSelectionAsync()方法將其顯示出來。
async void InputBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)/p>
p> {/p>
p> e.Handled =true; //來取消原來的調用,然后創(chuàng)建自己的上下文菜單,并在適當的地方調用剪貼板(clipboard)/p>
p> TextBox box = sender as TextBox;/p>
p> PopupMenu menu = new PopupMenu(); //創(chuàng)建PopupMenu菜單/p>
p> menu.Commands.Add(new UICommand("復制", null, 0));/p>
p> menu.Commands.Add(new UICommand("剪切", null, 1));/p>
p> menu.Commands.Add(new UICommand("粘貼", null, 2));/p>
p> menu.Commands.Add(new UICommand("全選", null, 3));/p>
p> menu.Commands.Add(new UICommand("刪除", null, 4));/p>
p> var cmd = await menu.ShowForSelectionAsync(GetPoint(box)); //這里的菜單位置也可以使用 new Rect(e.CursorLeft,e.CursorTop,0,0); /p>
p> if (cmd != null)/p>
p> { /p>
p> string text;/p>
p> DataPackage package;/p>
p> int index=(int)cmd.Id;/p>
p> switch (index)/p>
p> {/p>
p> case 0:/p>
p> text = box.SelectedText;/p>
p> package =new DataPackage();/p>
p> package.SetText(text);/p>
p> Clipboard.SetContent(package);/p>
p> break;/p>
p> case 1:/p>
p> text = box.SelectedText;/p>
p> box.SelectedText ="";/p>
p> package =new DataPackage();/p>
p> package.SetText(text); /p>
p> Clipboard.SetContent(package);/p>
p> break;/p>
p> case 2:/p>
p> text =awaitClipboard.GetContent().GetTextAsync();/p>
p> box.SelectedText = text; /p>
p> break;/p>
p> case 3:/p>
p> box.SelectAll();/p>
p> break;/p>
p> case 4:/p>
p> box.SelectedText ="";/p>
p> break;/p>
p> }/p>
p> }/p>
p> }
3.使用另一種方法,實現(xiàn)上下文菜單
1)先為TextBox注冊右鍵事件,右鍵事件必須使用下面的方法注冊,在頁面上寫RightTapped事件,是不起作用的。
public MainPage()/p>
p> {/p>
p> this.InitializeComponent();/p>
p> ContentText.AddHandler(RightTappedEvent, new RightTappedEventHandler(ContentText_RightTapped),true);/p>
p> }
2).ContentText_RightTapped方法
private async void ContentText_RightTapped(object sender, RightTappedRoutedEventArgs e)/p>
p> {/p>
p> //和 InputBox_ContextMenuOpening方法一樣/p>
p> }
UI層代碼:
Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
TextBox x:Name="InputBox" Margin="100,50,50,660" />
TextBox x:Name="ContentText" Margin="100,150,50,50"/>
/Grid>
注意:在上下文菜單中,最多可以添加6個command,當添加多余6個command時,會出現(xiàn)錯誤。
上下文菜單是非常好的方法:特別是為不可選的element提供交互,或者與鄰近的element進行交互。
如需源代碼,點擊ContextMenu_jb51net.zip下載