Hyperlink Support
RichTextEditor provides built-in support for creating and managing hyperlinks. Through the exposed commands related to hyperlinks, namely ApplyHyperlinkCommand
, RemoveHyperlinkCommand
, and OpenHyperlinkCommand
, users can manipulate the hyperlinks inside the RichTextEditor content.
In addition, RichTextEditorToolbar
exposes predefined toolbar items wired to the hyperlink commands.
-
RichTextEditorAddHyperlinkToolbarItem
—Opens a popup to enter URL information and executes an action that adds a hyperlink for the current selection. -
RichTextEditorAddOrEditHyperlinkToolbarItem
—Opens a popup to enter URL information and executes an action that adds or edits a hyperlink for the current selection. -
RichTextEditorHyperlinkNavigationToolbarItem
—This toolbar item adds or edits hyperlinks depending whether a hyperlink is selected. If a hyperlink is selected, the editor navigates to toolbar sub-items for operations related to a hyperlink. If a hyperlink is not selected, a popup with a dialog is displayed to enter URL information and add a hyperlink for the current selection.
Additional Toolbar Items for Hyperlink Operations
The RichTextEditorHyperlinkNavigationToolbarItem
has the following items:
-
RichTextEditorEditHyperlinkToolbarItem
—Edits the hyperlink from the current selection. -
RichTextEditorRemoveHyperlinkToolbarItem
—Removes the hyperlink from the current selection. -
RichTextEditorOpenHyperlinkToolbarItem
—Navigates to the URL.
The next image shows the RichTextEditorEditHyperlinkToolbarItem
on mobile platforms:
For more details, see the RichTextEditor Toolbar topic.
Methods
You can also take advantage of the following API related to hyperlinks:
-
GetHyperlinkAsync
method—Asynchronously returns aRichTextHyperlink
under the caret in the editor (ornull
if no hyperlink is present). TheRichTextHyperlink
object contains theUrl
andTitle
of the link.
Hyperlink Error Handling
In case users try to open invalid URLs (for example, the URL is not absolute, the domain does not exist or is incomplete, and so on), the following message is shown by default and indicates that the URL has an error:
You can override the default behavior by handling the RichTextEditor's OpenHyperlinkError
event:
-
OpenHyperlinkError
event—Raised when users try to open invalid URLs in the editor. TheOpenHyperlinkError
event handler receives two parameters:- The
Sender
which is the RichTextEditor control. -
OpenHyperlinkErrorEventArgs
provides the following properties:-
Error
—of typeSystem.Exception
, can be used to get the exact error message. -
Url
—of typestring
, defining the URL of the hyperlink. -
Handled
—boolean property indicating whether the event is handled.
-
- The
To change the default error handling behavior:
- Subscribe to the event.
- Set the
Handled
property of the event arguments toTrue
to prevent the default message. - Add a custom implementation.
The following example demonstrates how to use the OpenHyperlinkError
event:
<telerik:RadRichTextEditor x:Name="richTextEditor"
OpenHyperlinkError="RichTextEditor_OpenHyperlinkError"
Grid.Row="{OnIdiom Desktop=1, Phone=0}" />
The next snippet shows the event handler which shows a custom message:
private void RichTextEditor_OpenHyperlinkError(object sender, OpenHyperlinkErrorEventArgs e)
{
e.Handled = true;
Application.Current.MainPage.DisplayAlert(string.Format("Error opening {0}", e.Url), e.Error.Message, "Ok");
}
For a runnable example with the RichTextEditor Hyperlink Error Handling, see the SDKBrowser Demo Application and go to RichTextEditor > Events.