Alex
Alex

Reputation: 261

TextBlock refuses ~70% of the time to make a hyperlink

Really really odd problem, in short, I'm doing a foreach over every word in a textblock, if that word starts with for example "@" I want to make a username hyperlink out of it. However in about 70% of the cases it replaces the text fine, but it just doesn't become a hyperlink.

Partial code:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    var kaas = Tweet.Split(' ');
    foreach (string a in kaas)
    {
        if (a.StartsWith("@"))
        {
            Hyperlink uname = new Hyperlink();
            uname.NavigateUri = new Uri("http://twitter.com/" + "xarinatan");
            uname.RequestNavigate += new RequestNavigateEventHandler(Hyperlink_RequestNavigateEvent);
            uname.Inlines.Add("ASDAS");
            TweetBlock.Inlines.Add(uname);
            //TweetBlock.Inlines.Add(Username(a));
            TweetBlock.Inlines.Add(" ");
        }
    }
}

Above code turns all instances that start with "@" into "ASDAS" but fails most of the time to properly convert it to a hyperlink, HOWEVER it DOES convert it SOMETIMES. It's completely beyond me how it only works sometimes, instead of all the time or not at all.

All suggestions are welcome!

edit: To clarify, it -always- replaces the text with 'ASDAS', but in 70% of the cases it doesn't become a hyperlink.

Upvotes: 1

Views: 161

Answers (1)

Alex
Alex

Reputation: 261

My friend found the answer. Somehow inlines being added as string causes sporadic behavior, they have to be added as a 'Run'.

Fix can be found here: https://github.com/zahndy/o3o/commit/68b50f8c0ea106bcc709d3f69658b28da9c8a9d4#diff-3

Thanks all for the suggestions!

Upvotes: 0

Related Questions