简体   繁体   English

Xamarin Forms 从 ViewModel 调用命令

[英]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:后面有这个 C# 代码:

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现在我想从我的 ViewModel 调用一个 ICommand,看起来像这样

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#?我知道如何用按钮来做到这一点,只是Command="Binding FlipCommand" ,但我怎么能用 C# 中的标签来做到这一点?

Assuming the page is bound to the ViewModel it can also be done in xaml:假设页面绑定到 ViewModel,它也可以在 xaml 中完成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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