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!