How to create hyperlinks in .NET MAUI projects
This article introduces the knowledge of "how to create hyperlinks in a .NET MAUI project". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In the .NET MAUI Preview 13 preview, .NET MAUI supports formatted text with tagged controls.
Formatted text in tags
A label is a view that displays text with or without text wrapping. Using the formatted text feature (now in a single tag), you can use different span elements to select multiple options for each setting.
For example, you can apply separate colors to words in a single label. This will make the label more decorative.
The Span element supports the following options:
CharacterSpacing
FontAttributes
FontFamily
FontSize
TextColor
TextTransform.Lowercase
TextTransform.Uppercase
TextDecorations.Underline
TextDecorations.Strikethrough
Create a hyperlink UI using the formatted text feature of the tag
I'll use two options, TextColor and TextDecorations.Undercomings.Undercoming, to create a tag with a hyperlink UI.
Create a reusable hyperlink class
A class named HyperlinkUI is created, which is derived from span, and a bindable property named LinkUrl is added to it.
Because span inherits GestureElement, you can add a Gesture recognizer to navigate using the LinkUrl attribute.
See the following code example.
Public class HyperlinkUI: Span {public static readonly BindableProperty LinkUrlProperty = BindableProperty.Create (nameof (LinkUrl), typeof (string), typeof (HyperlinkUI), null); public string LinkUrl {get {return (string) GetValue (LinkUrlProperty);} set {SetValue (LinkUrlProperty, value);}} public HyperlinkUI () {ApplyHyperlinkAppearance ();} void ApplyHyperlinkAppearance () {this.TextColor = Color.FromArgb ("# 0000EE") This.TextDecorations = TextDecorations.Underline;} void CreateNavgigationCommand () {/ / because Span inherits GestureElement, you can add Gesture Recognizer to use LinkUrl for navigation}}
You can now use this hyperlink UI as a span element in the tag. We can display the whole text or part of the text as hyperlinked text. See the following code example.
That's all for "how to create hyperlinks in a .NET MAUI project". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!