繁体   English   中英

Xamarin Forms 从 ViewModel 调用命令

[英]Xamarin Forms call command from ViewModel

我有个问题。 我创建了一个这样的标签:

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

后面有这个 C# 代码:

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

现在我想从我的 ViewModel 调用一个 ICommand,看起来像这样

public ICommand FlipCommand => new Command(Flip);

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

假设页面绑定到 ViewModel,它也可以在 xaml 中完成:

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

暂无
暂无

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

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