gpt4 book ai didi

c# - 在 axml 中使用来自 pcl 的 resx 字符串?

转载 作者:太空狗 更新时间:2023-10-29 14:15:10 24 4
gpt4 key购买 nike

我在 MyAppResources 类的可移植类库中有用于本地化的 resx 文件。所以在代码中我可以通过以下方式获取本地化字符串:

 View.FindViewById<Button>(Resource.Id.btnCacheClear).Text = MyAppResources.TextClearCache;

但是否还有一种方法可以在 axml 中设置这个字符串?

 <Button
android:id="@+id/btnCacheClear"
android:text= ?? />

谢谢,汤姆

最佳答案

我认为最干净的方法是定义一个自定义属性,该属性将通过 ResourceManager 从您的 PCL 资源中读取。

我正在使用 MvvmCross,并让它与自定义语言绑定(bind)解析器一起工作:

public class CustomLanguageBindingParser : MvxBindingParser , IMvxLanguageBindingParser
{
protected override MvxSerializableBindingDescription ParseBindingDescription()
{
this.SkipWhitespace();

string resourceName = (string)this.ReadValue();

// Pass the resource name in as the parameter on the StringResourceConverter.
return new MvxSerializableBindingDescription
{
Converter = "StringResource",
ConverterParameter = resourceName,
Path = null,
Mode = MvxBindingMode.OneTime
};
}

public string DefaultConverterName { get; set; }

public string DefaultTextSourceName { get; set; }
}

还有一个转换器:

public class StringResourceConverter : IMvxValueConverter
{
private static ResourceManager manager = new ResourceManager(typeof(AppResources));

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Ignore value. We are using parameter only.
return manager.GetString((string)parameter);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

在您的 MvxAndroidSetup 类中注册您的解析器:

    protected override void InitializeIoC()
{
base.InitializeIoC();

Mvx.RegisterType<IMvxLanguageBindingParser, CustomLanguageBindingParser>();
}

在您的 .axml 中,定义一个命名空间 xmlns:local="http://schemas.android.com/apk/res-auto"

并调用资源。例如在 TextView 上:local:MvxLang="Text MyResourceKey"

这与 MvvmCross 绑定(bind)系统 Hook 。第一部分“文本”确定目标属性,而第二部分被解析为资源键。语言绑定(bind)解析器将其转换为与您的自定义转换器的绑定(bind),并将键作为转换器参数。转换器根据转换器参数进行字符串查找。

关于c# - 在 axml 中使用来自 pcl 的 resx 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22843561/

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