英文:
.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();
}
复现步骤
- 在Android设备/模拟器上运行项目
- 展开底部的展开器
- 尝试点击任何一个按钮
- 不会触发任何提示消息
公共复现项目存储库链接
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
- Run the project on an Android device/emulator
- Expand the expander at the bottom
- Try to click on either button
- No toast is fired
Link to public reproduction project repository
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:
<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论