C# 在axml中使用来自pcl的resx字符串?
我在MyAppResources类的可移植类库中有用于本地化的resx文件。因此,在代码中,我可以通过以下方法获得本地化字符串:C# 在axml中使用来自pcl的resx字符串?,c#,android,localization,xamarin,portable-class-library,C#,Android,Localization,Xamarin,Portable Class Library,我在MyAppResources类的可移植类库中有用于本地化的resx文件。因此,在代码中,我可以通过以下方法获得本地化字符串: View.FindViewById<Button>(Resource.Id.btnCacheClear).Text = MyAppResources.TextClearCache; View.findviewbyd(Resource.Id.btnCacheClear).Text=MyAppResources.TextClearCache; 但是,是否
View.FindViewById<Button>(Resource.Id.btnCacheClear).Text = MyAppResources.TextClearCache;
View.findviewbyd(Resource.Id.btnCacheClear).Text=MyAppResources.TextClearCache;
但是,是否还有一种方法可以在axml中设置这个字符串
<Button
android:id="@+id/btnCacheClear"
android:text= ?? />
Thx,Tom使用字符串/Android Designer中RESX文件中的任何资源都不受支持,从代码中设置它们是目前唯一可行的方法。我认为最干净的方法是定义一个自定义属性,该属性将通过
ResourceManager
从PCL资源中读取
我正在使用MvvmCross,并使用了自定义语言绑定解析器:
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>();
}
protected override void InitializeIoC()
{
base.InitializeIoC();
Mvx.RegisterType();
}
在.axml中,定义一个名称空间xmlns:local=”http://schemas.android.com/apk/res-auto“
并调用资源。例如,在TextView上:local:MvxLang=“Text MyResourceKey”
这将钩住MvvmCross绑定系统。第一部分“文本”确定目标属性,而第二部分被解析为资源键。语言绑定解析器将其转换为一个绑定,使用自定义转换器和键作为转换器参数。转换器根据转换器参数进行字符串查找。我发现它在Designer中仅与android:text=“@string/TextClearCache”一起工作。该字符串取自PCL,甚至可以根据Designer中的语言进行适当更改。但它没有编译:错误“找不到与给定名称匹配的资源(在'text'处,值为'@string/TextClearCache'))