简体   繁体   English

XAML中的String.Format以及Xamarin形式的本地化

[英]String.Format in XAML with localization in Xamarin Forms

I need to get a string from View Model and another from Resx and concatenate them to show it in a label. 我需要从View Model中获取一个字符串,并从Resx中获取另一个字符串,并将它们连接起来以在标签中显示它。 I know I can do it from View model by creating a new property. 我知道我可以通过创建新属性在View模型中完成此操作。 However, I was wondering if there is a way to it in XAML itself. 但是,我想知道XAML本身是否有办法。 Something like this 像这样

  <Label Text="{Binding UserName, StringFormat='{i18n:Translate Welcome} {0}'}"/>

You can simply use a formatted string in your label, aiding yourself with spans and building up your Label Text, spans now allow you to do bindings in their text property so if you do the following it should work. 您可以简单地在标签中使用格式化的字符串,以帮助自己使用跨度并建立标签文本,跨度现在允许您在其text属性中进行绑定,因此,如果执行以下操作,它应该可以工作。

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="{i18n:Translate Welcome}"/>
            <Span Text="{Binding UserName}"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

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

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