You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET MAUI:如何获取上下文菜单位置并传递点击坐标

解决方案

你可以通过两种方式实现指定位置打开上下文菜单并传递点击坐标,以下是具体实现:

方法一:XAML定义菜单,代码指定位置显示

这种方式适合菜单结构固定的场景,在XAML中定义上下文菜单,然后在右键点击时指定位置显示,并将点击坐标存储供菜单事件使用。

修改后的XAML代码

<AbsoluteLayout
    Grid.Row="0"
    Background="Red"
    x:Name="AbsLayoutContainer" >
    <!-- 添加上下文菜单定义 -->
    <FlyoutBase.ContextFlyout>
        <MenuFlyout>
            <MenuFlyoutItem
                Text="Notiz hinzufügen"
                Clicked="OnAddNoteClicked" />
            <!-- 可添加更多菜单项 -->
        </MenuFlyout>
    </FlyoutBase.ContextFlyout>

    <Image
            x:Name="BackgroundImage"
            Source="blueprint1.jpg"
            AbsoluteLayout.LayoutBounds="0,0, 1, 1"
            AbsoluteLayout.LayoutFlags="SizeProportional" />

    <AbsoluteLayout.GestureRecognizers>
        <TapGestureRecognizer
                    Tapped="OnLayoutRightClick"
                    Buttons="Secondary" />
    </AbsoluteLayout.GestureRecognizers>
</AbsoluteLayout>

修改后的后台代码

// 存储当前右键点击的坐标
private Point? _currentTapLocation;

public void OnLayoutRightClick(object sender, TappedEventArgs e)
{
    _currentTapLocation = e.GetPosition(AbsLayoutContainer);
    if (_currentTapLocation != null)
    {
        // 在指定位置显示上下文菜单
        FlyoutBase.ShowAt(AbsLayoutContainer, (Point)_currentTapLocation);
    }
}

void OnAddNoteClicked(object sender, EventArgs e)
{
    if (_currentTapLocation != null)
    {
        // 在这里使用点击坐标
        double clickX = _currentTapLocation.Value.X;
        double clickY = _currentTapLocation.Value.Y;
        // 执行你的业务逻辑,比如在该位置添加笔记
    }
}

方法二:代码后台动态创建菜单并显示

这种方式适合需要动态生成菜单的场景,直接在代码中创建菜单并绑定事件时传递坐标。

修改后的后台代码

public void OnLayoutRightClick(object sender, TappedEventArgs e)
{
    Point? pointInAbsLayoutContainer = e.GetPosition((View)sender);
    DisplayContextMenu(pointInAbsLayoutContainer);
}

void DisplayContextMenu(Point? tapLocation)
{
    if (tapLocation == null)
        return;

    var menuFlyout = new MenuFlyout();

    // 创建菜单项并绑定点击事件,直接传递坐标
    var addNoteItem = new MenuFlyoutItem
    {
        Text = "Notiz hinzufügen"
    };
    addNoteItem.Clicked += (s, args) => OnAddNoteClickedWithLocation((Point)tapLocation);

    // 添加更多菜单项(示例)
    // var deleteItem = new MenuFlyoutItem { Text = "Löschen" };
    // deleteItem.Clicked += (s, args) => OnDeleteClickedWithLocation((Point)tapLocation);

    menuFlyout.Items.Add(addNoteItem);
    // menuFlyout.Items.Add(deleteItem);

    // 在指定位置显示菜单
    menuFlyout.ShowAt(AbsLayoutContainer, (Point)tapLocation);
}

void OnAddNoteClickedWithLocation(Point tapLocation)
{
    // 直接使用传递过来的坐标
    double clickX = tapLocation.X;
    double clickY = tapLocation.Y;
    // 执行你的业务逻辑
}

关键说明

  • FlyoutBase.ShowAt(View, Point) 方法用于在指定视图的相对坐标位置显示菜单,这里的坐标是从右键点击事件中获取的相对于 AbsLayoutContainer 的位置。
  • 两种方式都能实现美观的系统风格菜单,且都能获取到右键点击的坐标供后续逻辑使用。

内容的提问来源于stack exchange,提问作者Timpl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 13:56:14