Cayjo Eahn
Cayjo Eahn

Reputation: 1

Xamarin Forms - Binding a function to StackLayout TapGesture

In my view I have:

<ContentView.BindingContext>
    <vm:HomeViewModel />
</ContentView.BindingContext>

In that viewmodel I have:

    public void test(object sender, EventArgs e)
    {
        var x = 0;
    }

That's just for testing so I can hit a breakpoint, but I can't seem to Bind the function to the View:

                <StackLayout BackgroundColor="White">
                    <StackLayout.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding test }" />
                    </StackLayout.GestureRecognizers>

I've tried to hook it up with Tapped property as well, adding parentheses, I keep getting:

Severity Code Description Project File Line Suppression State Error Position 57:51. No method OnTapGestureRecognizerTapped with correct signature found on type

I know I'm missing something here, can anyone shed some light on my problem?

Upvotes: 0

Views: 417

Answers (1)

Chris Roeder
Chris Roeder

Reputation: 21

You need a command in your viewmodel binding to:

public ICommand cmdTest { get { return new Command(() => test())

Now in your view you are able to bind to cmdTest:

<TapGestureRecognizer Command="{Binding cmdTest}" />

Upvotes: 0

Related Questions