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
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
}
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
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With