1

Supposing we're having this case of a RichTextBlock :

<RichTextBlock IsTextSelectionEnabled="True">
    <Paragraph>
        <Run Text="{Binding Description}" />
    </Paragraph>
</RichTextBlock>

Is it possible to detect possible hyperlinks in the content so that when the user clicks them, the web browser app will open up?

For example, let's suppose the Description property would consist of the following string:

"Welcome to Stackoverflow. You can find a quick guide at http://www.stackoverflow.com/guide. To track your questions, please visit http://www.stackoverflow.com/questions. Happy coding!"

Currently this exact string is bound on the UI, but I would like somehow to be able to detect possible links and be able to navigate to them when tapping them, just as you can using a web browser.

The text selection set to disabled is one of the requirements I have to meet, altough that could help in some way to copy-paste the link, at least.

Any suggestions are highly appreciated!

VasileF
  • 2,856
  • 2
  • 23
  • 36
  • I have only one option, which is use regular expressions to find typical link code, like " – Mark Jan 10 '15 at 21:12
  • I've thought of a similar... processing approach but I thought there might be a solution already without much code-behind needed. But in any case, the string of the Description property is NOT in html format. It's plain text. – VasileF Jan 10 '15 at 21:14
  • who or how the link is constructed? it must have something to identify it. Its not so much code, but regex is "funny" :) – Mark Jan 10 '15 at 21:15
  • It's just a string bound on the UI, it is not built in any way. But if the string contains a sequence of characters which form a valid URL, to allow navigation to the link by tapping it. – VasileF Jan 10 '15 at 21:29
  • then use the same string to find the link and do whatever you want with it. – Mark Jan 10 '15 at 21:30
  • If the text is You have it! – Mark Jan 10 '15 at 21:30
  • I am trying to achieve something like this : http://blogs.u2u.be/diederik/post/2013/10/28/An-auto-hyperlinking-RichTextBlock-for-Windows-81-Store-apps.aspx. However, binding is not supported on that example... – VasileF Jan 10 '15 at 22:27
  • Maybe [this answer](http://stackoverflow.com/a/27742886/2681948) will help you - it's about *TextBlock* but should also work for *RichTextBlock*. – Romasz Jan 11 '15 at 11:55

1 Answers1

0

You need to parse whole HTML and hell no - don't do it with Regex like someone suggested (regular expression can't parse html) - install HtmlAgilityPack from Nuget manager (doesn't matter that is not well formatted html document, HtmlAgilityPack will do it's work).

Feel free to pick and tweak my XamlHtmlParser (not everything is all right at the moment - it's really eeearly dev stage but you can use few classes from it to parse hyperlinks, linebreaks etc..) from my open-source polish reddit-like service app implementation: https://github.com/thefex/Wykop/tree/dev_windows81_api/Wykop.ApiProvider/XamlParser.

If you want to your hyperlinks "navigate" add proper "NavigateUri" in HyperLinkNodeParser class.

Usage:

XamlHtmlParser htmlParser = new XamlHtmlParser(new DefaultHtmlNodeParserProvider());
RichTextBlock richTextBlock = ..;
var parsedHtmlBlocks = htmlParser.GetBlocksFromHtmlString(htmlLikeString);
richTextBlock.Blocks.Clear();

foreach (var parsedBlock in parsedHtmlBlocks)
   richTextBlock.Blocks.Add(parsedBlock);

Btw. it seems like binding is not possible - code-behind is must be.

fex
  • 3,488
  • 5
  • 30
  • 46
  • There is no HTML we speak about. Just some plain text like "Welcome to stackoverflow" or "Welcome to wwww.stackoverflow.com". In the 2nd case, I want "www.so.com" to be a hyperlink in the RichTextBlock. Why would regex be a bad approach in this case? – VasileF Jan 10 '15 at 23:37
  • @Vasilef oh so I didn't get it - sorry. If there is no html then regex is ok - find all and create Hyperlink Inlines elements from that. – fex Jan 10 '15 at 23:45
  • "regular expression can't parse html" regular expression CAN parse html, of course... – Mark Jan 11 '15 at 10:12
  • @Mark http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not – fex Jan 11 '15 at 13:58