【问题标题】:Xamarin XAML Multi language in StringFormatStringFormat 中的 Xamarin XAML 多语言
【发布时间】:2020-06-17 15:10:48
【问题描述】:

我在 Xamarin 中有一个简单的 (Android) 应用程序。 到目前为止,它使用单一语言,现在我添加了第二语言的翻译。我使用资源 .resx 文件,并像这样在 XAML 中使用它:

<Span Text="{x:Static resource:AppResources.Text1}" /> 

其中 Text1 从资源文件加载(取决于语言)。 我不知道如何在下一行使用 Binding 和 StringFormat 做类似的事情:

<Label Text="{Binding Datum, StringFormat='Some text: {0}'}" />

我试过了:

<Label Text="{Binding Datum, StringFormat='{x:Static resource:AppResources.Text2} {0}'}" />

但它没有用。

有什么想法吗?

【问题讨论】:

  • 您使用本地化资源翻译静态文本。如果您有动态文本,则该方法不起作用。 “基准”是什么类型的数据?
  • 基准(=英文日期)是字符串。
  • 如果动态文本不能这样使用,你有什么建议? XAML 中是否有任何可能的 IF(取决于设备语言)?
  • 如果是 DateTime,那么 .NET 将根据设备的区域设置为您进行本地化和格式化
  • 如果您需要在 DateTime 旁边的标签中添加一些本地化文本,您可以将其分解为跨度并仅使用文本的本地化资源

标签: xaml xamarin string-formatting


【解决方案1】:

使用 Spans 组合数据和文本

<Label>
  <Label.FormattedText>
    <FormattedString>
      <Span Text="{x:Static resource:AppResources.Text1}" />
      <Span Text="{Binding Datum}" />
    </FormattedString>
  </Label.FormattedText>
</Label>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多