本文介绍了点击 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 添加到 Border 并订阅 Tap事件.在事件处理程序中,您获得 BorderContextMenu,如果它没有逻辑父级,则将 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 而不是点击并按住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 07:37