How to set links in a text block that can be clicked in wp7

I have a text box containing links. Content in the text is created at runtime. My problem is that the links inside the text are not clickable, how to make all the links inside the text block interactive, so that when I click on the link that it should open a web browser. In android we can install it using autolink.Is this option available in wp7 or in wp7.1 mango?

+6
source share
3 answers

Use HyperLink .

<TextBlock> <Run>Pure Text</Run> <Hyperlink Command="{Binding HyperLinkTapped}">http://google.com</Hyperlink> <Run>Pure Text Again</Run> </TextBlock> 

This is supported by Windows Phone 7.1 (Mango).

If necessary, you can create your own FlowDocument from your data at run time.

An example of how to generate a FlowDocument from a string:

 private void OnMessageReceived(string message) { var textBlock = new RichTextBox() { TextWrapping = TextWrapping.Wrap, IsReadOnly = true, }; var paragraph = new Paragraph(); var runs = new List<Inline>(); foreach (var word in message.Split(' ')) { Uri uri; if (Uri.TryCreate(word, UriKind.Absolute, out uri) || (word.StartsWith("www.") && Uri.TryCreate("http://" + word, UriKind.Absolute, out uri))) { var link = new Hyperlink(); link.Inlines.Add(new Run() { Text = word }); link.Click += (sender, e) => { var hyperLink = (sender as Hyperlink); new WebBrowserTask() { Uri = uri }.Show(); }; runs.Add(link); } else { runs.Add(new Run() { Text = word }); } runs.Add(new Run() { Text = " "}); } foreach (var run in runs) paragraph.Inlines.Add(run); textBlock.Blocks.Add(paragraph); MessagesListBox.Children.Add(textBlock); MessagesListBox.UpdateLayout(); } 
+12
source

There are no built-in functions for this.

If your text (including links) is HTML, you can display it in a WebBrowser control.
If not, you will need to parse the text and create links yourself. (The combination of TextBlocks and HyperlinkButtons inside a WrapPanel is probably suitable for this.)

+2
source

In Silverlight, a RichTextBox contol can help you.

 <RichTextBox> <Paragraph> <Run Text="This have to navigate me to Google: "/> <Hyperlink NavigateUri="http://google.com" TargetName="_blank">google.com</Hyperlink> </Paragraph> </RichTextBox> 
+2
source

Source: https://habr.com/ru/post/898137/


All Articles