zoukankan      html  css  js  c++  java
  • Windows 8实用窍门系列:16.Windows 8的右键菜单

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

      然后我们看看注册两个控件的右键处理事件如下:

           /// <summary>
            /// Invoked when this page is about to be displayed in a Frame.
            /// </summary>
            /// <param name="e">Event data that describes how this page was reached.  The Parameter
            /// property is typically used to configure the page.</param>
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
                //上下文菜单
                this.tbName.ContextMenuOpening += tbName_ContextMenuOpening;
                //右键菜单
                this.gdMenu.RightTapped += gdMenu_RightTapped;
            }
    
            /// <summary>
            /// 右键菜单事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void gdMenu_RightTapped(object sender, RightTappedRoutedEventArgs e)
            {
                SetRightClick(sender);
            }
    
            /// <summary>
            /// 上下文菜单
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void tbName_ContextMenuOpening(object sender, ContextMenuEventArgs e)
            {
                SetRightClick(sender);
            }
    
            /// <summary>
            /// 事件卸载
            /// </summary>
            /// <param name="e"></param>
            protected override void OnNavigatedFrom(NavigationEventArgs e)
            {
                base.OnNavigatedFrom(e);
                //上下文菜单
                this.tbName.ContextMenuOpening -= tbName_ContextMenuOpening;
                //右键菜单
                this.gdMenu.RightTapped -= gdMenu_RightTapped;
            }

      再次看看前台Xaml代码如下:

        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" Name="gdMenu">
            <TextBox HorizontalAlignment="Left" Margin="141,164,0,0" TextWrapping="Wrap"
                     Name="tbName" Text="" VerticalAlignment="Top" Height="157" Width="540"/>
            <TextBox HorizontalAlignment="Left" Margin="210,353,0,0" TextWrapping="Wrap" 
                     Name="tbText" Text="" VerticalAlignment="Top" Width="290"/>
        </Grid>

      如需源码请点击 Win8Menu.rar 下载,下面是效果图.

  • 相关阅读:
    text-overflow:ellipsis; 使用
    js jquery jquery.wordexport.js 实现导出word
    CI框架程序--本地调试之后部署新浪SAE
    跳转页面的几种方式[归纳整理中......]
    使用jquery插件报错:TypeError:$.browser is undefined的解决方法
    apache Internal Server Error 的几个问题
    【总结整理】用户的需求分析:问对问题才能找准用户需求----摘自《人人都是产品经理》
    【总结整理】如何成为小白用户----摘自《人人都是产品经理》
    【总结整理双十一促销门道---摘自《人人都是产品经理》
    【总结整理】2018淘宝双11评价
  • 原文地址:https://www.cnblogs.com/chengxingliang/p/2880627.html
Copyright © 2011-2022 走看看