点击上下文菜单,而不是使用按钮控件点击并按住

发布于 2024-12-05 06:51:26 字数 2057 浏览 0 评论 0原文

我试图在这个问题上应用同样的想法解释 ContextMenu on tap instead of tap and hold to my application using按钮控制。

但是,在执行下面的代码时,我会得到nullrefrenceException

<Button Content="Menu" Margin="0,0,316,699" Grid.Row="1" x:Name="MenuButton" >
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Tap="GestureListener_Tap" />
    </toolkit:GestureService.GestureListener>
    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="Add to Favorite"  Click="AddFavorite_Click"/>
            <toolkit:MenuItem Header="Samples"  Click="Samples_Click"/>
            <toolkit:MenuItem Header="Send to friends"  Click="SendToFriends_Click"/>
            <toolkit:MenuItem Header="Links"  Click="Links_Click"/>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
</Button>

private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
    Button button = sender as Button;
    ContextMenu contextMenu = ContextMenuService.GetContextMenu(button);

    if (contextMenu.Parent == null)
    {

        contextMenu.IsOpen = true;
    } 
}

实际上,由于某种原因,只需将示例代码与边框控制使用带来相同的nullReferenceException。以下是我得到的堆栈,例外。

at Microsoft.Phone.Controls.ContextMenu.UpdateVisualStates(Boolean useTransitions)

at Microsoft.Phone.Controls.ContextMenu.OnOpened(RoutedEventArgs e)

at Microsoft.Phone.Controls.ContextMenu.<OpenPopup>b__12(Object s, EventArgs e)

at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, 
Object sender, Object args)

at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

有人可以帮助我如何使代码工作吗?我是Windows Phone应用程序开发的新手,因此任何帮助都将不胜感激!

I am trying to apply the same kind of idea explaind on this question
ContextMenu on tap instead of tap and hold to my application using button control.

However, I get NullRefrenceException when executing the code below.

<Button Content="Menu" Margin="0,0,316,699" Grid.Row="1" x:Name="MenuButton" >
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener Tap="GestureListener_Tap" />
    </toolkit:GestureService.GestureListener>
    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="Add to Favorite"  Click="AddFavorite_Click"/>
            <toolkit:MenuItem Header="Samples"  Click="Samples_Click"/>
            <toolkit:MenuItem Header="Send to friends"  Click="SendToFriends_Click"/>
            <toolkit:MenuItem Header="Links"  Click="Links_Click"/>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
</Button>

private void GestureListener_Tap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
{
    Button button = sender as Button;
    ContextMenu contextMenu = ContextMenuService.GetContextMenu(button);

    if (contextMenu.Parent == null)
    {

        contextMenu.IsOpen = true;
    } 
}

And actually, just using the sample code with Border control gives me the same NullReferenceException for some reason. Below is the stack I get with the exception.

at Microsoft.Phone.Controls.ContextMenu.UpdateVisualStates(Boolean useTransitions)

at Microsoft.Phone.Controls.ContextMenu.OnOpened(RoutedEventArgs e)

at Microsoft.Phone.Controls.ContextMenu.<OpenPopup>b__12(Object s, EventArgs e)

at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, 
Object sender, Object args)

at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Could anybody help me on how to get the code working? I am very new to Windows Phone app development, so any help will be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸印温柔 2024-12-12 06:51:26

这里同样的问题。

该错误是由此代码引起的:

private void UpdateVisualStates(bool useTransitions) 
[..]
_outerPanel.Orientation = Orientation.Vertical;

当时onapplyTemplate()尚未调用,导致_OUTERPANE l为null。

可以通过检查是否为空并重新编译工具包来解决问题。

不幸的是,微软拒绝修复问题

...

Same problem here.

The bug is caused by this code:

private void UpdateVisualStates(bool useTransitions) 
[..]
_outerPanel.Orientation = Orientation.Vertical;

at that point OnApplyTemplate() has not been called, causing _outerPanel to be null.

The problem can be resolved by checking if it is null and recompiling the toolkit.

Unfortunately Microsoft are refusing to fix the problem

...Stefan

夜吻♂芭芘 2024-12-12 06:51:26

我建议两件事:

1)silverlight工具包7.1中存在错误,因此当从保持事件以外的任何其他地方调用上下文菜单时,您会遇到异常。

<Button Content="Menu" Margin="0,0,316,699" Grid.Row="1" x:Name="MenuButton" Hold="MenuButton_Hold"/>

    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="Add to Favorite"  Click="AddFavorite_Click"/>
            <toolkit:MenuItem Header="Samples"  Click="Samples_Click"/>
            <toolkit:MenuItem Header="Send to friends"  Click="SendToFriends_Click"/>
            <toolkit:MenuItem Header="Links"  Click="Links_Click"/>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>

C# 代码没问题,因为只需将其复制到保持事件

2) 您不必将上下文菜单放入按钮括号中,然后返回到 7.0 silverlight 工具包。

<Button Content="Menu" Margin="0,0,316,699" Grid.Row="1" x:Name="MenuButton" Click="MenuButton_Click"/>

    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="Add to Favorite"  Click="AddFavorite_Click"/>
            <toolkit:MenuItem Header="Samples"  Click="Samples_Click"/>
            <toolkit:MenuItem Header="Send to friends"  Click="SendToFriends_Click"/>
            <toolkit:MenuItem Header="Links"  Click="Links_Click"/>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>

I would suggest two things:

1) There is bug in silverlight toolkit 7.1 so you get an exception when calling the context menu from anything else than hold event.

<Button Content="Menu" Margin="0,0,316,699" Grid.Row="1" x:Name="MenuButton" Hold="MenuButton_Hold"/>

    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="Add to Favorite"  Click="AddFavorite_Click"/>
            <toolkit:MenuItem Header="Samples"  Click="Samples_Click"/>
            <toolkit:MenuItem Header="Send to friends"  Click="SendToFriends_Click"/>
            <toolkit:MenuItem Header="Links"  Click="Links_Click"/>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>

C# code is ok as it is just copy it to the hold event

2) You dont have to put the context menu in to the button brackets, and go back to 7.0 silverlight toolkit.

<Button Content="Menu" Margin="0,0,316,699" Grid.Row="1" x:Name="MenuButton" Click="MenuButton_Click"/>

    <toolkit:ContextMenuService.ContextMenu>
        <toolkit:ContextMenu>
            <toolkit:MenuItem Header="Add to Favorite"  Click="AddFavorite_Click"/>
            <toolkit:MenuItem Header="Samples"  Click="Samples_Click"/>
            <toolkit:MenuItem Header="Send to friends"  Click="SendToFriends_Click"/>
            <toolkit:MenuItem Header="Links"  Click="Links_Click"/>
        </toolkit:ContextMenu>
    </toolkit:ContextMenuService.ContextMenu>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文