简体   繁体   中英

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#?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM