Samantha J T Star
Samantha J T Star

Reputation: 32838

Can I add a tap.SetBinding using Xamarin's new C# markup?

Here's what I am currently doing in C#:

        TapGestureRecognizer tap = new TapGestureRecognizer()
        {
            NumberOfTapsRequired = 1
        };
        tap.SetBinding(TapGestureRecognizer.CommandProperty, new Binding("DeckTapCommandAsync", source: GD));
        tap.SetBinding(TapGestureRecognizer.CommandParameterProperty, new Binding("TapCommandParam", source: GD));
        GestureRecognizers.Add(tap);

What I would like to know is if there is any way I can do this using Xamarin.Forms C# markup

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/csharp-markup

Upvotes: 0

Views: 101

Answers (1)

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10978

Yes, you could use SetBinding for TapGestureRecognizer.

Using ICommand:

var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.SetBinding (TapGestureRecognizer.CommandProperty, "TapCommand");
image.GestureRecognizers.Add(tapGestureRecognizer);

For more details, you could check the link: https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap#using-icommand

You could download the source file from the link: https://learn.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/workingwithgestures-tapgesture/

Or you could set with below:

// Image TRASH
            TapGestureRecognizer tgrTrash = new TapGestureRecognizer();
            tgrTrash.SetBinding(TapGestureRecognizer.CommandProperty, new Binding("BindingContext.TrashCommand", source: this));
            tgrTrash.SetBinding(TapGestureRecognizer.CommandParameterProperty, ".");

            Image imageTrash = new Image() { Source = "trash.png" };
            //Label lTrash = new Label { Text = "Trash", VerticalTextAlignment = TextAlignment.Center };
            imageTrash.GestureRecognizers.Add(tgrTrash);

You could download the source file from the link: https://github.com/WendyZang/TestBindingWithListView

Upvotes: 1

Related Questions