data-binding - Xamarin.Forms 中标签 StringFormat 的本地化

标签 data-binding xamarin.forms localization label string-formatting

在 xamarin 表单中,我可以将文本本地化为如下标签:<Label Text="{x:Static resources:AppResources.Text}"/>
使用资源的命名空间:<ContentView ... xmlns:resources="clr-namespace:ProjectName.Resources;assembly=ProjectName">
我还可以绑定(bind)一些值并向标签添加字符串格式:<Label Text="{Binding Value, StringFormat='The value is: {0}' }"/>
问题是文本 值为:没有本地化。

谁可以同时做两个,绑定(bind)一个值并本地化 StringFormat?

最佳答案

我在 Localizing XAML 找到了答案

我必须添加文本 值为:{0} 到资源文件。
我需要为翻译添加一个 IMarkupExtension。我将该类添加到与资源文件相同的命名空间中。

[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
    private readonly CultureInfo _ci;

    static readonly Lazy<ResourceManager> ResMgr = new Lazy<ResourceManager>(
        () => new ResourceManager(typeof(AppResources).FullName, typeof(TranslateExtension).GetTypeInfo().Assembly));

    public string Text { get; set; }

    public TranslateExtension()
    {
        if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
        {
            _ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
        }
    }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        if (Text == null)
            return string.Empty;

        return ResMgr.Value.GetString(Text, _ci) ?? Text;
    }
}

并像这样使用它:
<Label Text="{Binding Value, StringFormat={resources:Translate LabelTextTheValueIs}}" />

关于data-binding - Xamarin.Forms 中标签 StringFormat 的本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824782/

相关文章:

html - Angular 绑定(bind)到文件输入字段的选择?

data-binding - SAPUI5:在 Controller 中检索模型对象

java数据绑定(bind)到gui,就像在c#中一样

mvvm - View 未在可绑定(bind)布局 xamarin 表单中刷新

ListView Fast Scroll 在 DataTemplate 中卡住互联网图像上的应用程序

ios - 如何在 xib 本地化之间切换

javascript - 为什么我的数据绑定(bind)不起作用?

xamarin - 如何在xamarin中隐藏导航工具栏图标?

c# - ASP.NET - 转换本地化数字

javascript - Highcharts 日期时间本地化