ContextMenu的使用

ContextMenu的使用

下面代码的效果是右键单击图片时,显示菜单。当单击菜单的某项时,执行相应的命令。

Image.RightTapped += new  RightTappedEventHandler(Image_RightTapped);

 

async void  Image_RightTapped( object  sender, RightTappedRoutedEventArgs e)
{
     var  menu = new  PopupMenu();
     menu.Commands.Add( new  UICommand( "Open with" , (command) =>
         {
             Display.Text = "'"  + command.Label + "' selected" ;
         }));
     menu.Commands.Add( new  UICommand( "Save attachment" , (command) =>
     {
         Display.Text = "'"  + command.Label + "' selected" ;
     }));
 
     var  chosenCommand = await menu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
     if  (chosenCommand == null )
     {
         Display.Text = "Context menu dismissed" ;
     }
}

  

Rect GetElementRect(FrameworkElement element)
{
     GeneralTransform buttonTransform = element.TransformToVisual( null );
     Point point = buttonTransform.TransformPoint( new  Point());
     return  new  Rect(point, new  Size(element.ActualWidth, element.ActualHeight));
}

  效果图:

 

2、文本的ContextMenuOpening事件

Scenario2TextBox.ContextMenuOpening += new ContextMenuOpeningEventHandler(Scenario2TextBox_ContextMenuOpening);

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" , null , 1));
     menu.Commands.Add( new  UICommandSeparator());
     menu.Commands.Add( new  UICommand( "Highlight" , null , 2));
     menu.Commands.Add( new  UICommand( "Look up on dictionary" , null , 3));
 
     // 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" ;
     }
}

  效果图:

效果是右键文本框时,弹出菜单。如上图。

 


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/03/27/2403972.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android Java中使用ContextMenu,您需要按照以下步骤进行操作: 1. 在您的Activity类中,重写onCreateContextMenu()方法。这个方法负责创建ContextMenu的菜单项。例如: ```java @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.context_menu, menu); } ``` 2. 在您的Activity类中,为您想要添加ContextMenu的View注册上下文菜单。例如,如果您想要在一个TextView上显示ContextMenu: ```java TextView textView = findViewById(R.id.my_text_view); registerForContextMenu(textView); ``` 3. 创建一个XML文件来定义ContextMenu的菜单项。在res/menu目录下创建一个名为context_menu.xml的文件,并添加所需的菜单项。例如: ```xml <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_item1" android:title="Menu Item 1" /> <item android:id="@+id/menu_item2" android:title="Menu Item 2" /> </menu> ``` 4. 在您的Activity类中,重写onContextItemSelected()方法来处理上下文菜单项的选择。例如: ```java @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_item1: // 处理菜单项1的选择 return true; case R.id.menu_item2: // 处理菜单项2的选择 return true; default: return super.onContextItemSelected(item); } } ``` 通过以上步骤,您就可以在Android Java中使用ContextMenu了。当用户长按指定的View时,将显示上下文菜单,并可以选择相应的菜单项执行相应操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值