程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> Windows 8開發入門(十六) Windows 8的右鍵菜單

Windows 8開發入門(十六) Windows 8的右鍵菜單

編輯:關於C#

在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>

如需源碼請下載:http://files.cnblogs.com/chengxingliang/Win8Menu.rar, 下面是效果圖.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved