CommunityToolkit / Maui

The .NET MAUI Community Toolkit is a community-created library that contains .NET MAUI Extensions, Advanced UI/UX Controls, and Behaviors to help make your life as a .NET MAUI developer easier
https://learn.microsoft.com/dotnet/communitytoolkit/maui
MIT License
2.27k stars 401 forks source link

[BUG] `TapGestureRecognizer` not triggered when in combination with `TouchBehavior` and `Border.Shadow` #2165

Open FroggieFrog opened 2 months ago

FroggieFrog commented 2 months ago

Is there an existing issue for this?

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

A TapGestureRecognizer is not triggered when used in combination with a TouchBehavior and inside of a Border with a Shadow.

Expected Behavior

The TapGestureRecognizer is triggered

Steps To Reproduce

  1. run the app from the repo in Android 14
  2. click on the images
  3. see that there is no Toast when clicked on the second image

Link to public reproduction project repository

https://github.com/FroggieFrog/BugMauiShadowTouch

Environment

- .NET MAUI CommunityToolkit: 9.0.3
- OS: Android 14
- .NET MAUI: 8.0.82

Anything else?

maui_bug

bijington commented 2 months ago

Why can't you just use the TouchGestureCompleted event from the TouchBehavior and not bother with a TapGestureRecognizer?

FroggieFrog commented 2 months ago
  1. I need to execute a Command -> TouchBehavior has a Command property, but BindingContext is not set
  2. I can't attach a EventToCommandBehavior to another Behavior
  3. I want to avoid code behind
  4. I can't think of any reason why my wanted behavior should not be possible
bijington commented 2 months ago

I agree with point 4 but I was just trying to find a simpler solution for you. Can you set the BindingContext of the TouchBehavior?

FroggieFrog commented 2 months ago

Unfortunately no. As soon as I add either BindingContext or Command to the toolkit:TouchBehavior, the app crashes. (screenshot and stacktrace are from WinUI, but same behavior on Android) I don't even have to add the Border to make it crash.

Stacktrace

Message = "Operation is not valid due to the current state of the object."

at Microsoft.Maui.Controls.Binding.d27.MoveNext() at System.Threading.Tasks.Task.<>c.b128_0(Object state) at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.b__0()

grafik

my code

 <Border
     Grid.RowSpan="2"
     Grid.Column="1"
     Margin="0,40,0,0"
     HorizontalOptions="Center">
     <Border.Behaviors>
         <toolkit:TouchBehavior
             BindingContext="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:MyViewModel}}}"
             Command="{Binding MyCommand}"
             HoveredOpacity="0.8"
             HoveredScale="0.9"
             PressedOpacity="0.6"
             PressedScale="0.8" />
     </Border.Behaviors>
     <!--<Border.GestureRecognizers>
         <TapGestureRecognizer Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodels:MyViewModel}}, Path=MyCommand}" />
     </Border.GestureRecognizers>-->

     <Image Source="{Binding MyModel, Converter={StaticResource myConverter}}" />
 </Border>
hansmbakker commented 2 months ago

As soon as I add either BindingContext or Command to the toolkit:TouchBehavior, the app crashes. (screenshot and stacktrace are from WinUI, but same behavior on Android) I don't even have to add the Border to make it crash.

Stacktrace Message = "Operation is not valid due to the current state of the object."

@FroggieFrog that one could be this issue - https://github.com/CommunityToolkit/Maui/issues/1783