c# - 如何扩展 stacklayout 的点击区域以占据视单元的高度?

标签 c# xaml xamarin xamarin.forms

所以我在 XAML 中有以下代码片段:

<TableView Intent="Settings">
   <TableRoot>
      <TableSection Title="Card Front Side">
         <ViewCell>
            <StackLayout x:Name="englishSide" Padding="20,0,20,0"
                         BackgroundColor="Red"
                         Orientation="Horizontal" VerticalOptions="Center">
               <Label Text="English" XAlign="Center"/>
               <Image x:Name="englishImage" Source="check.png" 
                      HorizontalOptions="EndAndExpand" IsVisible="false" />
            </StackLayout>
          </ViewCell>
       </TableSection>
   </TableRoot>
</TableView>

在我的 C# 代码中,我有以下代码来处理点击事件:

englishSide.GestureRecognizers.Add(new TapGestureRecognizer
{
   NumberOfTapsRequired = 1,
   Command = new Command(() =>
   {
       englishImage.IsVisible = true;
       App.Database.UpdateSettings((int)Lang.English, "CardFrontSide");
   })
});

现在,当我点击该行时,我注意到它没有立即响应我的点击。然后我意识到可以应用点击事件的区域并没有占据行的整个高度,如下图所示(红色背景的区域)。

enter image description here

谁能告诉我如何让它占据整个行/ViewCell 高度而不改变 ViewCell 的高度?谢谢

最佳答案

您的StackLayoutVerticalOptions设置为“Center”,这意味着“看到那边的空间了吗?只需获取中间的某个地方谢谢你”。
这意味着 StackLayout 不会占用超出所需的空间。

如果你想让StackLayout填充单元格的高度(由ListView.RowHeight定义),你需要将其设置为FillAndExpand > (您可以尝试不同的值,我不确定 CenterAndExpandFill 是否足够)。

如果 CenterAndExpand 不起作用,您可以将 StackLayoutVerticalOptions="Center" 包装在另一个 StackLayout 中> 使用 FillAndExpand 使文本保持垂直居中:

<TableView Intent="Settings">
   <TableRoot>
      <TableSection Title="Card Front Side">
         <ViewCell>
            <StackLayout VerticalOptions="FillAndExpand"
                         x:Name="englishSide"
                         Padding="20,0,20,0"
                         BackgroundColor="Red">
              <StackLayout Orientation="Horizontal" VerticalOptions="CenterAndExpand">
                 <Label Text="English" XAlign="Center"/>
                 <Image x:Name="englishImage" Source="check.png" 
                        HorizontalOptions="EndAndExpand" IsVisible="false" />
              </StackLayout>
            </StackLayout>
          </ViewCell>
       </TableSection>
   </TableRoot>
</TableView>

关于c# - 如何扩展 stacklayout 的点击区域以占据视单元的高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39814212/

相关文章:

c# - 如果登录不正确,如何使 C# WCF session 无效

c# - 将产品代码传递给 MSI : This installation is forbidden by system policy

c# - C#中C++ DLL的直接内存访问

wpf - 将 Enum 转换为 TextBlock 文本中的字符串

c# - 为什么我不能将早期的 xamarin 表单版本作为 nuget 添加到我的 Xamarin 表单项目/android 包中?

c# - 内存快照触发dotMemory中的Garbage collection

c# - 命令(ICommand)和点击事件的区别

xaml - 为什么覆盖 ApplicationPageBackgroundThemeBrush 不起作用?

c# - 使用 Xamarin for Visual Studio 调试正在运行的 Android 应用程序

Xamarin Forms - 从模式返回对象