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";
            }
        }

  效果圖:

效果是右鍵文本框時,彈出菜單。如上圖。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM