AndreiMisiukevich / CardView

CardsView | CarouselView | CoverflowView | CubeView for Xamarin.Forms
MIT License
712 stars 114 forks source link

TapGestureRecognizer Tapped create a build error #369

Closed grounch closed 3 years ago

grounch commented 3 years ago

Hi just noticed an issue with tapgestures. If i want to assign an event handler to tapped event of tapgesture on an element nested in a CarouselView, it gives the following error at build:

Error: Common Language Runtime detected an invalid program

        <CarouselView>
            <CarouselView.ItemsSource>
                <x:Array Type="{x:Type ContentView}">
                    <ContentView >
                        <Grid>
                            <Grid.GestureRecognizers>
                                <TapGestureRecognizer Tapped="GestureRecognizer_Tapped"/>
                            </Grid.GestureRecognizers>
                        </Grid>
                    </ContentView>
                </x:Array>
            </CarouselView.ItemsSource>
        </CarouselView>

Works well with the Command="..." property to bind a command from the viewmodel, but i need to use an eventhandler in the view's .cs .

AndreiMisiukevich commented 3 years ago

I propose you to bind TapGestureRecognizer.Command to your view's command.

<ContentPage x:Name="Page"

...

<Grid.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding TapCommand, Source={x:Reference Page}}"/>
</Grid.GestureRecognizers>

Then in code-behind add TapCommand and handle click)

AndreiMisiukevich commented 3 years ago

Feel free to ask more questions :)