简体   繁体   English

WP7-ContextMenu在超链接上不显示

[英]WP7 - ContextMenu doesn't show on Hyperlink

I'm trying to programmatically add a ContextMenu to a Hyperlink. 我正在尝试以编程方式将ContextMenu添加到超链接。 I've searched in the documentation and in forums, and seems that the correct solution should be this: 我在文档和论坛中进行了搜索,似乎正确的解决方案应为:

var link = new Hyperlink();
link.Inlines.Add(new Run() { Text = linkText });
link.FontWeight = FontWeights.Bold;
link.TargetName = linkText;
link.Click += new RoutedEventHandler(link_Click);

ContextMenu menu = new ContextMenu();
MenuItem item = new MenuItem();
item.Click += new RoutedEventHandler(CopyLink);
item.Header = "copy link";
item.Tag = linkText;
menu.Items.Add(item);
ContextMenuService.SetContextMenu(link, menu);

This code compiles and does not throw any exception. 此代码将编译,并且不会引发任何异常。 I've tested and the ContextMenu is indeed added to the Hyperlink. 我已经测试过,并且ContextMenu确实已添加到超链接中。 The problem is that it will not show anytime. 问题是它不会随时显示。 I can tap & hold the link all the time of the world and the menu will not appear. 我可以一直按住此链接,菜单不会出现。 Also tried to add a listener with GestureService and GestureListener, but the Hold event does not fire at all. 还尝试使用GestureService和GestureListener添加侦听器,但Hold事件根本不会触发。

Anyone can help me? 有人可以帮助我吗? Thanks. 谢谢。

You can't do ContextMenu s on Hyperlink s . 您无法在Hyperlink上执行ContextMenu You can do it on a HyperlinkButton , though. 不过,您可以在HyperlinkButton上执行此操作。 I'm not exactly sure of the reason, but it does work. 我不确定原因,但确实可以。 Depending on what you're trying to do, HyperlinkButton may have been what you wanted anyway ( Hyperlink is usually only used inside documents of text). 根据您要执行的操作, HyperlinkButton可能一直是您想要的Hyperlink通常仅在文本文档内部使用)。

I just found the answer while reading the Windows Phone 7.5 Unleashed book by Daniel Vaughan. 在阅读Daniel Vaughan撰写的Windows Phone 7.5 Unleashed书时,我才找到了答案。 My problem was that I needed to add a hyperlink in a RichTextBox, and I can only use Inlines to show text. 我的问题是我需要在RichTextBox中添加超链接,并且只能使用内联显示文本。 Hyperlink is an Inline, but does not support ContextMenu. 超链接是一个内联,但不支持ContextMenu。 As Tim suggested, I needed to use HyperlinkButton. 正如Tim所建议的,我需要使用HyperlinkBut​​ton。 The solution is that there is a class called InlineUIContainer . 解决方案是有一个名为InlineUIContainer的类。 So, this 所以这

var inline = new InlineUIContainer { Child = SomeHyperlinkButton }

made the trick for me. 帮了我大忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM