A. Vreeswijk
A. Vreeswijk

Reputation: 954

Xamarin Forms call command from ViewModel

I have a problem. I created a label like this:

<Label HorizontalOptions="Center" VerticalOptions="FillAndExpand">
    <Label.FormattedText>
        <FormattedString>
            <Span Text="Don't have an account? " TextColor="White"/>
            <Span Text="Sign Up."
    TextColor="White"
    FontAttributes="Bold"
    x:Name="lblSignUp" />
        </FormattedString>
    </Label.FormattedText>
</Label>

With this C# code behind it:

lblSignUp.GestureRecognizers.Add(new TapGestureRecognizer()
{
    Command = new Command(async () =>
    {
        //Call function from ViewModel
    })
});

Now I want to call a ICommand from my ViewModel that looks like this

public ICommand FlipCommand => new Command(Flip);

I know how to do that with a button, just Command="Binding FlipCommand", but how can I do that with a label in C#?

Upvotes: 1

Views: 1072

Answers (1)

Nick Peppers
Nick Peppers

Reputation: 3251

Assuming the page is bound to the ViewModel it can also be done in xaml:

<Label.GestureRecognizers>
    <TapGestureRecognizer Command="{Binding FlipCommand}"/>
</Label.GestureRecognizers>

Upvotes: 4

Related Questions