You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin触发Grid点击时如何同时执行绑定Command与Button点击事件

你提供的XAML代码如下:

<Grid.GestureRecognizers>
  <TapGestureRecognizer NumberOfTapsRequired="1" Command="{Binding Source={RelativeSource AncestorType={x:Type locals:OneViewModel}}, Path=OneTappedView}" CommandParameter="{Binding .}" />
</Grid.GestureRecognizers>

<Button x:Name="bt_one" Clicked="bt_one_Clicked"/>

触发逻辑结论

默认配置下,Grid的Tap手势绑定的Command和按钮的bt_one_Clicked事件不会同时执行,分两种场景对应不同触发结果:

  • 点击Grid未被Button覆盖的空白区域:仅触发OneTappedView Command,不会触发按钮点击事件,因为点击动作没有命中Button控件。
  • 点击Button自身所在区域:仅触发bt_one_Clicked点击事件,不会触发Grid的Tap手势Command。因为.NET MAUI/Xamarin.Forms 中按钮类控件默认会接收并处理点击事件,阻止事件向上冒泡到父容器,父级的手势识别器无法捕获这次点击动作。

实现同时执行的方案

如果确实需要两个逻辑同时运行,不需要修改手势或控件的基础配置,直接在bt_one_Clicked事件处理方法中,手动调用ViewModel中OneTappedView命令的Execute方法即可。


内容的提问来源于stack exchange,提问作者Chim Di Tru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 05:45:03