开发者

ContextMenu on tap instead of tap and hold with Button Control

开发者 https://www.devze.com 2023-04-04 21:55 出处:网络
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.

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 ho开发者_开发技巧w to get the code working? I am very new to Windows Phone app development, so any help will be appreciated!


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


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消