Create a hyperlink using Xamarin.Forms (xaml and c#)

jnel899 picture jnel899 · Jun 2, 2016 · Viewed 22.6k times · Source

I am a relatively inexperienced professional programmer (I'm only 20 years old). Therefore, I apologize in advance as there may be some larger concepts that I do not fully grasp yet. I hope this is an appropriate question to ask, as an hour of googling could not help me.

I basically want to create a hyperlink in Xamarin.Forms using the label class. Basically, I want to following link to take the user to google.com in a web browser:

<Label Text="http://www.google.com/" />

I can't find anything in the Xamarin Forms API about this and the internet has vague and limited information on this topic in Xamarin.Forms.

Is this possible? If so, could someone please point me in the right direction? Thanks in advance to anyone who answers.

Answer

Jason picture Jason · Jun 2, 2016

You can't really do this because Labels by default don't respond to user input, but you can achieve something similar with gestures

using Xamarin.Forms;
using Xamarin.Essentials;

Label label = new Label();
label.Text = "http://www.google.com/";

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += async (s, e) => {
    // Depreciated - Device.OpenUri( new Uri((Label)s).Text); 
    await Launcher.OpenAsync(new Uri(((Label)s).Text));
};
label.GestureRecognizers.Add(tapGestureRecognizer);