1

I'm building a chat application and I am parsing out links and adding them to the chatroom view with Hyperlinks. These Hyperlinks have the following event handler set to them:

    void urlLink_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            Hyperlink link = sender as Hyperlink;
            this.chatUI.DisplayWebBrowser(link.NavigateUri);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }

Whether or not the event gets triggered seems to be random though. Some links will work as intended when clicked, while some links will not work. Some links work to start with but after more messages are received, they stop working. Anyone know what's the cause and how to solve this? This is how the Hyperlink control is being created:

newSpan = new Span();
Run urlRun = new Run();
urlRun.Text = urlMatches[i].Value;
Hyperlink urlLink = new Hyperlink();
urlLink.NavigateUri = new Uri(urlMatches[i].Value, UriKind.Absolute); // Add URI to the Hyperlink control
urlLink.Click += urlLink_Click; // Add event handler to control
urlLink.Inlines.Add(urlRun); // Add text to Hyperlink

1 Answer 1

1

I had the same issue. By some trial and error, I fixed it... by assigning a dummy empty command to the Hyperlink. Don't ask me to explain, I still haven't managed to understand how it could fix anything, but in my case it did.

First, declare a DummyCommand class:

/// <summary>
/// Workaround for the hyperlink click issue. What the hell is going on?
/// </summary>
public class DummyCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return true;
    }

    public void Execute(object parameter)
    {
    }

    public event EventHandler CanExecuteChanged;
}

Then, when creating a Hyperlink, make sure to assign the DummyCommand to the Command property:

newSpan = new Span();
Run urlRun = new Run();
urlRun.Text = urlMatches[i].Value;
Hyperlink urlLink = new Hyperlink();
urlLink.NavigateUri = new Uri(urlMatches[i].Value, UriKind.Absolute); // Add URI to the Hyperlink control
urlLink.Click += urlLink_Click; // Add event handler to control
urlLink.Command = new DummyCommand(); // Workaround for the Click not been triggered
urlLink.Inlines.Add(urlRun); // Add text to Hyperlin

Then scratch your head a bit, and move on to something else.

2
  • This worked. I'm scratching my head indeed, thank very much KooKiz! Commented Mar 22, 2014 at 15:56
  • Also worked for me. When the dummy command is not attacted, none of events is fired. But when attached, both the click event AND the commands execute method is fired.
    – mslot
    Commented May 8, 2014 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.