轻按ContextMenu,而不是按住 [英] ContextMenu on tap instead of tap and hold

查看:98
本文介绍了轻按ContextMenu,而不是按住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打开一个菜单,并且由于WP7不是设计的才能执行此类操作,因此我正在使用Toolkit。以下是示例代码:

I need to open up a menu and since WP7 is not designed to perform such actions, I am taking help of Toolkit. Following is the sample code:

<Border BorderThickness="3" Padding="6">
     <toolkit:ContextMenuService.ContextMenu>
         <toolkit:ContextMenu>
             <toolkit:MenuItem Header="item1" Click="Item1_Click" />
             <toolkit:MenuItem Header="item2" Click="Item2_Click" />
             <toolkit:MenuItem Header="item3" Click="Item3_Click" />
         </toolkit:ContextMenu>
     </toolkit:ContextMenuService.ContextMenu>
     <TextBlock Text="Tap" />
</Border>

现在,只要用户按下并按住操作,此方法就可以正常工作。但是我不能要求用户采取这种措施。我需要在单击/轻击/触摸/手势(无论您想调用它的位置)上显示菜单。有人可以建议吗?如果您认为工具包不是最好的方法,那么请使用示例代码建议替代方法。我尝试了弹出窗口,但这对我的应用程序来说弊大于利。

Now this works fine as long as user does a press and hold action. But I can't ask the user for such action. I need to display the menu on a single click/tap/touch/gesture (watever you want to call it). Can someone please suggest? If you think toolkit is not the best way, then please suggest alternatives with sample code. I tried popup but that did more bad than good to my applicaiton

推荐答案

您可以添加 GestureListener 并订阅 Tap 事件。在事件处理程序中,为 Border 获取 ContextMenu 并设置 IsOpen 如果没有逻辑父级,则为true。

You could add GestureListener to the Border and subscribe to the Tap event. In the event handler, you get the ContextMenu for the Border and set IsOpen to true if it doesn't have a logical parent.

<Border BorderThickness="3" Padding="6">
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Tap="GestureListener_Tap" />
    </toolkit:GestureService.GestureListener>
    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="item1" Click="Item1_Click" />
            <toolkit:MenuItem Header="item2" Click="Item2_Click" />
            <toolkit:MenuItem Header="item3" Click="Item3_Click" />
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
    <TextBlock Text="Tap" />
</Border>

private void GestureListener_Tap(object sender, GestureEventArgs e)
{
    Border border = sender as Border;
    ContextMenu contextMenu = ContextMenuService.GetContextMenu(border);
    if (contextMenu.Parent == null)
    {
        contextMenu.IsOpen = true;
    }
}

这篇关于轻按ContextMenu,而不是按住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆