Nick Heiner
Nick Heiner

Reputation: 122530

Hyperlink.Click not being executed

I'm writing a Windows Phone Mango app. I have a hyperlink that contains email addresses. I want it to show a message box on tap:

Hyperlink href = new Hyperlink();
href.Click += (s, r) =>
{
    MessageBox.Show("href clicked");
};
// add href to RichTextBox

When I click on the hyperlink in the emulator, nothing happens. The click += line is hit in a debugger, but the new EmailComposeTask() line isn't.

What am I doing wrong? Is Click not the event I want?

Update: I switched EmailComposeTask to MessageBox to verify that this issue isn't related to the email compose task not running on the emulator. I've reproduced this issue on my device.

Upvotes: 0

Views: 558

Answers (3)

Nick Heiner
Nick Heiner

Reputation: 122530

The following code works:

            GestureService.GetGestureListener(href); // adding this makes it work
            href.Click += (s, r) =>
            {
                 MessageBox.Show("href clicked");
                 new EmailComposeTask() { To = entireLink.Value }.Show();

            };
            href.Inlines.Add(new Run() { Text = entireLink.Value });

GetGestureListener creates a gesture listener if it doesn't already exist.

Upvotes: 2

Medeni Baykal
Medeni Baykal

Reputation: 4333

Following code is runs on device. text is RichTextBox object.

var linkText = new Run() { Text = "Link text" };
var link = new Hyperlink();
link.Inlines.Add(linkText);
link.Click += (o, e) =>
{
    MessageBox.Show("Link clicked");
};

var paragraph = new Paragraph();
paragraph.Inlines.Add(link);

text.Blocks.Add(paragraph);

Upvotes: 0

Waleed
Waleed

Reputation: 3145

You should use HyperlinkButton Class, Hyperlink is a class used with rich text document rendering.

Upvotes: 0

Related Questions