如何在Xamarin.Forms中实现可点击标签

时间:2019-02-18 12:49:55

标签: xamarin xamarin.forms

我正在尝试将我的提交按钮交换为可点击的标签。达到此目标的最佳方法是什么,或者甚至可以使用Xamarin.Forms来实现?目前,这是我的按钮代码:

 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());
      }
 }

当前,这是我的标签代码:

<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" />

我是否可以添加一个标签点击手势,使我能够执行此操作?我想从我的应用程序中删除按钮,而只需轻按一下手势即可使用标签来完成相同的操作。

2 个答案:

答案 0 :(得分:1)

当然,只需在您的TapGestureRecognizer中添加一个Label。这样做:

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

答案 1 :(得分:0)

@Gerald的答案没错,但是如果您要通过编程方式进行操作,可以这样做

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

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