我试图在我的应用程序中使用按钮控件将同一种想法解释到这个问题上 ContextMenu on tap instead of tap and hold。ContextMenu点击而不是点击并按住按钮控制
但是,执行下面的代码时我得到了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应用程序开发非常陌生,所以任何帮助将不胜感激!
哪一行给出错误? –
我在上面添加了堆栈跟踪。行结束后我会得到这个异常。我一定在做一些非常愚蠢的事情。 – Meimio