每行不同的 TapGestureRecognizer

Different TapGestureRecognizer for each row

<ContentView>
    <Grid InputTransparent="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <StackLayout Spacing="0"
                 Grid.Row="0"
                 VerticalOptions="Center">
            <Label Text="Algebra"/>
            <Label Text="number 1" />
        </StackLayout>
        <Switch IsToggled="True"
            VerticalOptions="Center"
                Grid.Row="1"
            Grid.Column="1"
                />

        <StackLayout Spacing="0"
                 Grid.Row="1"
                 VerticalOptions="Center">
            <Label Text="Arithmetics" />
            <Label Text="number 2" />
        </StackLayout>
        <Switch IsToggled="True"
            VerticalOptions="Center"
            Grid.Column="1"
                />
    </Grid>
</ContentView>

我有两行,每行有 2 个标签和一个开关。如何为每一行调用两个不同的 Tap 事件。像这样:

<ROW1?.GestureRecognizers>
            <TapGestureRecognizer Tapped="OnTapped1" />
        </ROW?.GestureRecognizers>


<ROW2?.GestureRecognizers>
            <TapGestureRecognizer Tapped="OnTapped2" />
        </ROW?.GestureRecognizers>

我是新手,但在网格中切换开关似乎不起作用,这很奇怪。

UI 图片:

由于您不能简单地将手势应用于整个网格行,

我决定只为 UI 使用 Switch 内置功能。

我所做的就是为每个 Switch 添加两种不同的方法:

<Grid InputTransparent="True">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <StackLayout Spacing="0"
             Grid.Row="0"
             VerticalOptions="Center">
        <Label Text="Algebra"/>
        <Label Text="number 1" />
    </StackLayout>
    <Switch IsToggled="True"
            Toggled="method2"
        VerticalOptions="Center"
            Grid.Row="1"
        Grid.Column="1"
            />

    <StackLayout Spacing="0"
             Grid.Row="1"
             VerticalOptions="Center">
        <Label Text="Arithmetics" />
        <Label Text="number 2" />
    </StackLayout>
    <Switch IsToggled="True"
        Toggled="method2"
        VerticalOptions="Center"
        Grid.Column="1"
            />
</Grid>