Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
674 views
in Technique[技术] by (71.8m points)

Xamarin.Forms Why is my tap command not working?

I have this kind of tap command in my carousel view:

                                                        <Image
                                                         x:Name="img_adPic" 
                                                         HeightRequest="150"
                                                         Aspect="AspectFill"
                                                         HorizontalOptions="FillAndExpand"
                                                         VerticalOptions="FillAndExpand"
                                                         Source="{Binding thumbnail}">
                                                            <Image.GestureRecognizers>
                                                                <TapGestureRecognizer Command="{Binding  Path=BindingContext.TapCommand, Source={x:Reference page}}"  CommandParameter="{Binding  Path=BindingContext.CurrentIndex, Source={x:Reference page}}"/>
                                                            </Image.GestureRecognizers>

                                                        </Image>

I gave my contentpage a name :

<ContentPage x:Name="page" >

Now in my class:

  public ICommand CarouselItemTapped{ get; set; }


    public Page_MainMenuDetail()
    {
        InitializeComponent();

        CarouselItemTapped = new Xamarin.Forms.Command((selectItem) =>
        {

        });

        instance = this;

     
    }

But no matter what I try, the command is NEVER fired. I think i did everything as in the tutorials, why is this not working?

Thank you

question from:https://stackoverflow.com/questions/65901003/xamarin-forms-why-is-my-tap-command-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could refer to the code below about how to use the TapCommand. The command would be triggled when you tap on the image. Xaml:

<ContentPage.Content>
    <AbsoluteLayout>
        <CarouselView
            x:Name="carouselView"
            AbsoluteLayout.LayoutBounds="0,0,1,1"
            AbsoluteLayout.LayoutFlags="All"
            IndicatorView="indicatorview">
            <CarouselView.ItemTemplate>
                <DataTemplate>
                    <Image
                        x:Name="img_adPic"
                        Aspect="AspectFill"
                        HeightRequest="150"
                        HorizontalOptions="FillAndExpand"
                        Source="{Binding .}"
                        VerticalOptions="FillAndExpand">
                        <Image.GestureRecognizers>
                            <TapGestureRecognizer Command="{Binding Path=BindingContext.TapCommand, Source={x:Reference page}}"  />
                        </Image.GestureRecognizers>
                    </Image>
                </DataTemplate>
            </CarouselView.ItemTemplate>

        </CarouselView>
        <IndicatorView
            x:Name="indicatorview"
            AbsoluteLayout.LayoutBounds=" 0.5,0.95,100,100"
            AbsoluteLayout.LayoutFlags=" PositionProportional"
            IndicatorColor=" LightGray"
            IndicatorSize=" 10"
            SelectedIndicatorColor=" Black" />
    </AbsoluteLayout>
</ContentPage.Content>

Code behind:

  public partial class Page2 : ContentPage
{
    public ICommand CarouselItemTapped { get; set; }
    public ICommand TapCommand { get; set; }
    public Page2()
    {
        InitializeComponent();
        var list = new List<string>
        {
            "pink.jpg",
            "pink.jpg",
            "pink.jpg",
            "pink.jpg",
            "pink.jpg",
        };
        carouselView.ItemsSource = list;
        //CarouselItemTapped = new Xamarin.Forms.Command((selectItem) =>
        //{

        //});
        //instance = this;
        TapCommand = new Command(commandEvent);
        this.BindingContext = this;
    }
    public void commandEvent()
    {

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...