【问题标题】:How to implement the row clicked in Xamarin Forms TableView Cell?如何实现 Xamarin Forms TableView Cell 中点击的行?
【发布时间】:2022-02-02 23:36:18
【问题描述】:

我在 XAML 中为 Xamarin 表单设置了以下表格视图:

<TableView Intent="Menu">
   <TableRoot>
      <TableSection Title="Categories">
         <ViewCell>
            <StackLayout Orientation="Horizontal" VerticalOptions="Center" Padding="20,0,20,0">
               <Label Text="Category" XAlign="Center"/>
               <Label Text=">" HorizontalOptions="EndAndExpand" XAlign="Center"/>
            </StackLayout>
        </ViewCell>
      </TableSection>
   </TableRoot> 
</TableView>

谁能告诉我如何实现单击该行以打开另一个内容页面 xamarin 表单?还是必须在iOS端单独实现?

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    查看有关 TapGestureRecognizer 的 Xamarin 文档。

    您也可以在此处应用该方法。 我不完全确定您是否可以将其直接应用于ViewCell,但您应该可以在StackLayout 上执行此操作。

    所以应该是这样的:

    <TableView Intent="Menu">
       <TableRoot>
          <TableSection Title="Categories">
             <ViewCell>
                <StackLayout Orientation="Horizontal" VerticalOptions="Center" Padding="20,0,20,0">
                   <StackLayout.GestureRecognizers>
                       <TapGestureRecognizer
                           Tapped="OnTapGestureRecognizerTapped"
                           NumberOfTapsRequired="1" />
                   </StackLayout.GestureRecognizers>
    
                   <Label Text="Category" XAlign="Center"/>
                   <Label Text=">" HorizontalOptions="EndAndExpand" XAlign="Center"/>
                </StackLayout>
            </ViewCell>
          </TableSection>
       </TableRoot> 
    </TableView>
    

    然后当然是实现事件方法。

    【讨论】:

      【解决方案2】:

      您还可以将 Tapped 方法添加到 ViewCell 本身。

      像这样:

      <ViewCell Tapped="Handle_Cell_Tapped">
      </View>
      

      然后在后面的代码中编写您的 Handle_Cell_Tapped 事件处理程序。

      【讨论】:

        【解决方案3】:

        您可以使用 Xamarin.Comunity.Toolkit 中的 ItemTappedEventArgsConverter 来执行此操作。这非常简单直接。这是文档的链接,还有一些示例。

        Xamarin Community Toolkit - ItemTappedEventArgsConverter

        【讨论】:

          猜你喜欢
          • 2017-05-07
          • 2021-12-06
          • 1970-01-01
          • 2021-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多