Xamarin 如何在ViewCell中创建一个标签,单击该标签将调用一个方法?

Xamarin 如何在ViewCell中创建一个标签,单击该标签将调用一个方法?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有以下代码: <ViewCell x:Name="ss" Height="50"> <Grid VerticalOptions="CenterAndExpand" Padding="20, 0"> <Label Style="{DynamicResource ListItemTextStyle}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Text="Catego

我有以下代码:

<ViewCell x:Name="ss" Height="50">
    <Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
        <Label Style="{DynamicResource ListItemTextStyle}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Text="Category Group" />
        <Switch x:Name="ssSwitch" HorizontalOptions="End" VerticalOptions="Center" Grid.Column="1" Toggled="SsSwitch" />
    </Grid>
</ViewCell>

我想用另一行和一个带有“Clear Deck”文本的标签来扩展它


我怎样才能为它添加一个标签,当点击它时会调用一个方法?

下面是一个简单的例子。您只需要添加一个带有
TapGestureRecognizer
的标签。然后您需要在代码隐藏中实现
ClearLabelTapped

<ViewCell x:Name="ss" Height="50">
  <StackLayout Orientation="Vertical">
    <Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
      <Label Style="{DynamicResource ListItemTextStyle}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Text="Category Group" />
      <Switch x:Name="ssSwitch" HorizontalOptions="End" VerticalOptions="Center" Grid.Column="1" Toggled="SsSwitch" />
    </Grid>
    <Label Text="Clear Deck">
      <Label.GestureRecognizers>
        <TapGestureRecognizer Tapped="ClearLabelTapped" />
      </Label.GestureRecognizers>
    </Label>
  </StackLayout>
</ViewCell>

请注意,使用视图模型和命令将是一种更好的方法(分离关注点),但我希望保持这种简单。请参阅上的官方文档。

以下是一个简单的示例,说明如何执行此操作。您只需要添加一个带有
TapGestureRecognizer
的标签。然后您需要在代码隐藏中实现
ClearLabelTapped

<ViewCell x:Name="ss" Height="50">
  <StackLayout Orientation="Vertical">
    <Grid VerticalOptions="CenterAndExpand" Padding="20, 0">
      <Label Style="{DynamicResource ListItemTextStyle}" HorizontalOptions="StartAndExpand" VerticalOptions="Center" Text="Category Group" />
      <Switch x:Name="ssSwitch" HorizontalOptions="End" VerticalOptions="Center" Grid.Column="1" Toggled="SsSwitch" />
    </Grid>
    <Label Text="Clear Deck">
      <Label.GestureRecognizers>
        <TapGestureRecognizer Tapped="ClearLabelTapped" />
      </Label.GestureRecognizers>
    </Label>
  </StackLayout>
</ViewCell>
请注意,使用视图模型和命令将是一种更好的方法(分离关注点),但我希望保持这种简单。请参阅有关的官方文档