.NET MAUI `Button.OnClicked`事件在点击时不会触发。

huangapple go评论73阅读模式
英文:

.NET MAUI `Button.OnClicked` Event Does Not Fire When Clicked / Tapped

问题

在代码后台添加了Clicked事件处理程序,并将其添加到XAML中的Button之后,当点击按钮时,HandleClicked事件处理程序从未触发。

是什么导致Button.Clicked事件不触发的问题?

MainPage.xaml

<Button x:Name="AddButton"
  Clicked="HandleClicked"
  Text="Add"
  HorizontalOptions="Start"
  VerticalOptions="End"
  WidthRequest="100">
  <Button.GestureRecognizers>
    <PointerGestureRecognizer PointerEntered="PointerGestureRecognizer_PointerEntered" />
  </Button.GestureRecognizers>
</Button>

MainPage.xaml.cs

async void HandleClicked(object? sender, EventArgs e)
{
  var toast = CommunityToolkit.Maui.Alerts.Toast.Make("Add has been clicked");
  await toast.Show();
}

复现步骤

  1. 在Android设备/模拟器上运行项目
  2. 展开底部的展开器
  3. 尝试点击任何一个按钮
  4. 不会触发任何提示消息

公共复现项目存储库链接

https://github.com/glenn2223/Community-Toolkit-Mail-Android-Expander-Issue

英文:

After adding a Clicked event handler in the code behind, and adding it to my Button in XAML, the HandleClicked event handler never fires when tapping the button.

What have I done wrong to cause the Button.Clicked event not to fire?

MainPage.xaml

<Button x:Name="AddButton"
  Clicked="HandleClicked"
  Text="Add"
  HorizontalOptions="Start"
  VerticalOptions="End"
  WidthRequest="100">
  <Button.GestureRecognizers>
    <PointerGestureRecognizer PointerEntered="PointerGestureRecognizer_PointerEntered" />
  </Button.GestureRecognizers>
</Button>

MainPage.xaml.cs

async void HandleClicked(object? sender, EventArgs e)
{
  var toast = CommunityToolkit.Maui.Alerts.Toast.Make("Add has been clicked");
  await toast.Show();
}

Steps To Reproduce

  1. Run the project on an Android device/emulator
  2. Expand the expander at the bottom
  3. Try to click on either button
  4. No toast is fired

https://github.com/glenn2223/Community-Toolkit-Mail-Android-Expander-Issue

答案1

得分: 0

The issue is that you've added a GestureRecognizer to your Button which is overriding the Clicked action.

When you remove the GestureRecognizer, the Clicked event fires:

<Button x:Name="AddButton"
        Clicked="AddClicked"
        Text="Add"
        HorizontalOptions="Start"
        VerticalOptions="End"
        WidthRequest="100">
</Button>

I do not know if this behavior is intended by the .NET MAUI engineering team. I recommend opening this Issue on the .NET MAUI GitHub Repo.

英文:

The issue is that you've aded a GestureRecognizer to your Button which is overriding the Clicked action.

When you remove the GestureRecognizer, the Clicked event fires:

  &lt;Button x:Name=&quot;AddButton&quot;
          Clicked=&quot;AddClicked&quot;
          Text=&quot;Add&quot;
          HorizontalOptions=&quot;Start&quot;
          VerticalOptions=&quot;End&quot;
          WidthRequest=&quot;100&quot;&gt;
  &lt;/Button&gt;

I do not know if this behavior is intended by the .NET MAUI engineering team. I recommend opening this Issue on the .NET MAUI GitHub Repo.

huangapple
  • 本文由 发表于 2023年6月12日 06:33:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定