RichTextBox is familiar to everyone. You can use hyperlink to add hyperlinks.
The following code implements hyperlinks.
<RichTextBox IsReadOnly="True">
<Paragraph>
Displaying text with
<Hyperlink NavigateUri="http://www.sohu.com">hyperlink</Hyperlink> .
</Paragraph>
</RichTextBox>
However, in Windows Phone, if the link is an external URL, the navigation fails, because webbrowsertask must be used to start the browser in Windows Phone.
Okay, so let's modify and use webbrowsertask. Some people may say this is a good option to enable a webbrowsertask In the click event of hyperlink.
Well, that's a good solution. But do you want to handle the click events of every hyperlink? No, we have another method.
If the navigation fails in debug mode, the system will jump to the void rootframe_navigationfailed (Object sender, navigationfailedeventargs e) function of APP. XAML. CS. Let's rebuild it in him.
void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
if (JDCommonFun.IsUrl(e.Uri.ToString()))
{
WebBrowserTask task = new WebBrowserTask();
task.Uri = e.Uri;
task.Show();
}
else
{
if (System.Diagnostics.Debugger.IsAttached)
{
// A navigation has failed; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
}
There is also a function above to match whether it is a URL
public static bool IsUrl(string str_url)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_url, @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
}
In this way, your hyperlink link can be started.
Write it here today, and write it another day to identify the URLs in a lot of text and display hyperlinks in RichTextBox.