点击上下文菜单,而不是使用按钮控件点击并按住
我试图在这个问题上应用同样的想法解释 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里同样的问题。
该错误是由此代码引起的:
当时
onapplyTemplate(
)尚未调用,导致_OUTERPANE
l为null。可以通过检查是否为空并重新编译工具包来解决问题。
不幸的是,微软拒绝修复问题
...
Same problem here.
The bug is caused by this code:
at that point
OnApplyTemplate(
) has not been called, causing_outerPane
l 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
我建议两件事:
1)silverlight工具包7.1中存在错误,因此当从保持事件以外的任何其他地方调用上下文菜单时,您会遇到异常。
C# 代码没问题,因为只需将其复制到保持事件
2) 您不必将上下文菜单放入按钮括号中,然后返回到 7.0 silverlight 工具包。
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.
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.