gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 14:38:35 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com