Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a clickable label to open an external url using external browser in Xamarin.Forms?

Isn't there anything like a LinkButton in Xamarin?

I want to create a label with the looks of an url link that once tapped opens the external browser.

I also need to know how to open the device's external browser (to open an specified url) in a Portable Project.

Thanks

like image 471
Dpedrinha Avatar asked May 12 '16 03:05

Dpedrinha


1 Answers

The Xamarin.Forms way to open an URL string in the default mobile browser:

Device.OpenUri(new Uri("http://example.com"))

While a Forms' Label does not have a click event, you can add a TapGestureRecognizer so when the label is tapped it executes Device.OpenUri.

Example:

var myURLLabel = new Label
{
    Text = "https://xamarin.com"
};

myURLLabel.GestureRecognizers.Add(new TapGestureRecognizer
{
    Command = new Command(() => {
        Device.OpenUri(new Uri(myURLLabel.Text));
    })
});

Xamarin-Forms-Labs' ExtendedLabel allows you style a label's text as underlined....

Ref: https://github.com/XLabs/Xamarin-Forms-Labs/wiki/ExtendedLabel

like image 153
SushiHangover Avatar answered Oct 12 '22 11:10

SushiHangover