Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HyperlinkButton in C# XAMARIN.FORMS

I want to create Label with click possibility like in WIN phone xaml

<HyperlinkButton Content="My Text to click"/>

Is there a possibility to do it in Xamarin.Forms?

I found this but is not the same:

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

like image 738
senzacionale Avatar asked Jan 12 '16 09:01

senzacionale


3 Answers

I'd take a much more standard approach and use a Button. Just set the background to match your app background and remove the border. Then there's no need for extra TapGestureRecongniser code. (Pseudo Code below:)

Xaml:

<Button Text="Click Me!" Background= "YourAppBackground" BorderWidth="0" Clicked="OnButtonClicked" />

Code-Behind:

void OnButtonClicked(object sender, EventArgs args)
{
    //Open your link in here
}
like image 77
KidCode Avatar answered Nov 16 '22 21:11

KidCode


I would suggest using GestureRecognizers and adding a Tap Gesture to a label. Ref: here

var label = new Label()
{
  Text="My Hyperlink"
};
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => {
    // handle the tap
};
label.GestureRecognizers.Add(tapGestureRecognizer);

GestureRecognizer is a public property on the View class which Label inherits from. See here

like image 38
User1 Avatar answered Nov 16 '22 19:11

User1


XAML code would be as follows:

<Label
    Text="My Text to click"
    HorizontalOptions="Center" >

    <Label.GestureRecognizers>
        <TapGestureRecognizer
            Tapped="OnLabelTapped"
            NumberOfTapsRequired="2" />
    </Label.GestureRecognizers>
</Label>

Note: By default, NumberOfTapsRequired is 1.

Then in your .cs file, add the method OnLabelTapped.

public void OnLabelTapped(object sender, EventArgs args)
{
    // Your code here
    // Example:
    // DisplayAlert("Message", "You clicked on the label", "OK");
}
like image 4
Curiousity Avatar answered Nov 16 '22 19:11

Curiousity