zoukankan      html  css  js  c++  java
  • 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/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    关于进程exit后,内存释放释放的实践
    《C语言基础日常笔记》
    阿里巴巴重磅推出,让你的电脑变成云服务器,只要 5 分钟! 5 分钟! 5 分钟!
    vs2010下使用绘图控件MsChart的方法
    归纳整理Linux下C语言常用的库函数----文件操作
    归纳整理Linux下C语言常用的库函数----字符串转换、字符测试、及内存控制
    参考 generate-parentheses
    初步整理数仓知识 2017
    论文学习 数码相机系统
    论文学习:数码相机处理器的结构设计
  • 原文地址:https://www.cnblogs.com/linlf03/p/2403972.html
Copyright © 2011-2022 走看看