Swetha
Swetha

Reputation: 9

In .Net MAUI TapGestures are not working in WIndows

In .Net MAUI 8.0.3 and 8.0.6 Tap gestures are not working in windows but works fine in Andriod and IOS. Please help if any additional registration need to be done

Tap Gestures are not triggering in windows MAUI. tried giving Buttons as secondary and primary both but no use

Upvotes: -1

Views: 1362

Answers (2)

Leon Lu
Leon Lu

Reputation: 9224

I tried to use tap gestures in Collectionview and ListView by .NET8 MAUI on Windows platform, it can run normally. Could you please add your code that caused the problem? You can try my tap gestures sample as a reference:

MainPage.xaml

<CollectionView x:Name="itemCollectionView"
                ItemsSource="{Binding Items}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout>
                <Label Text="{Binding Name}" />
                <!-- Button with TapGestureRecognizer -->
                <Button Text="Click Me">
                    <Button.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding Source={x:Reference itemCollectionView}, Path=BindingContext.TapCommand}"
                                            CommandParameter="{Binding .}" />
                    </Button.GestureRecognizers>
                </Button>
            </StackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    public ObservableCollection<ItemViewModel> Items { get; set; }
    public ICommand TapCommand { get; set; }
    public MainPage()
    {
        InitializeComponent();

        Items = new ObservableCollection<ItemViewModel>
        {
            new ItemViewModel { Name = "Item 1" },
            new ItemViewModel { Name = "Item 2" },
            new ItemViewModel { Name = "Item 3" }
        };
        TapCommand = new Command<ItemViewModel>(OnItemTapped);
        BindingContext = this;
    }

    private void OnItemTapped(ItemViewModel item)
    {
        // Handle item tapped event
        // You can perform actions based on the tapped item
        DisplayAlert("Item Tapped", $"You tapped on {item.Name}", "OK");
    }
}
public class ItemViewModel
{
    public string Name { get; set; }
}

For more instructions for tap gestures, you can refer to Recognize a tap gesture

Upvotes: 0

MrThree
MrThree

Reputation: 46

Since you did not provide any sample code, I created a new MAUI application in VS2022 on .NET 8.0.3 and modified this code in the pre-generated sample code in MainPage.xaml:

<Button
    x:Name="CounterBtn"
    Text="Click me" 
    SemanticProperties.Hint="Counts the number of times you click"
    Clicked="OnCounterClicked"
    HorizontalOptions="Fill">

    <Button.GestureRecognizers>
        <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped" />
    </Button.GestureRecognizers>

</Button>

In the MainPage.xaml.cs file, I added only this:

private void TapGestureRecognizer_Tapped(object sender, TappedEventArgs e)
{ 
    count++; 
}

I then started the project in Windows and when the button was clicked, both the generated OnCounterClicked event and the TapGestureRecognizer_Tapped event added by me were called and the variable count was incremented by 2.

In my experience, some xaml components override Tap events or rather some components do not pass Taps to the component above or below. I had this problem on iOS, where I had to specify that the component must pass the user's Tap to other components that are in it.

Upvotes: 0

Related Questions