MarkPW
MarkPW

Reputation: 140

How do I implement a tappable label in Xamarin.Forms

Im trying to swap my submit button to a tappable label. What is the best way to go to achieve this or is this even possible using Xamarin.Forms? Currently this is the code I have for my button:

 async void SubmitFeedback_Clicked(object sender, EventArgs e)
 {
      ExtendedGrialButton btn = sender as ExtendedGrialButton;

      if (btn.IDValue != IDofSymptomforAdjusting)
      {
           await DisplayAlert("Add feedback", "Please add feedback for the symptom selected", "OK");
      }

      else if (rangeSlider == null)
      {
           await DisplayAlert("Add feedback", "Please add feedback for a symptom", "OK");
      }
      else
      {
           await AddSymptomFeedback(rangeSlider.IDValue, rangeSlider.Value.ToString());
      }
 }

Currently this is my label code :

<Label Style="{StaticResource FontIcon}"
       TextColor="#8472AF"
       VerticalOptions="Center"
       HorizontalOptions="Center"
       HeightRequest="160"
       WidthRequest="160"
       FontSize="60"
       VerticalTextAlignment="Center"
       x:Name="SymptomIcon"
       Margin="0,0,0,0"
       Grid.Row="1"
       Grid.RowSpan="4"    
       Grid.Column="2" />

Is it possible for me to add a label tapped gesture that will allow me to do this? I'd like to remove the button from my app and just use the label with a tapped gesture to do the same thing.

Upvotes: 0

Views: 212

Answers (2)

Arvind Chourasiya
Arvind Chourasiya

Reputation: 17422

@Gerald answer isn't wrong but if you want to do it programmatically, can do like this

public MainPage()
{
    InitializeComponent();
    var tapGestureRecognizer = new TapGestureRecognizer();
    tapGestureRecognizer.Tapped += async(s, e) => {

      //your code
    };
    SymptomIcon.GestureRecognizers.Add(tapGestureRecognizer);
}

Upvotes: 0

Gerald Versluis
Gerald Versluis

Reputation: 34013

Sure, just add a TapGestureRecognizer to your Label. Do it like this:

<Label ...>
    <Label.GestureRecognizers>
        <TapGestureRecognizer Tapped="SubmitFeedback_Clicked" />
    </Label.GestureRecognizers>
</Label>

Upvotes: 1

Related Questions