work hard work smart

专注于Java后端开发。 不断总结,举一反三。
随笔 - 1158, 文章 - 0, 评论 - 153, 阅读 - 186万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

ContextMenu的使用

Posted on   work hard work smart  阅读(11557)  评论(0编辑  收藏  举报

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

  效果图:

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

 

  
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示