在Xamarin XAML中将转换器与Translate i18n一起使用 [英] Using converter with Translate i18n in xamarin xaml
本文介绍了在Xamarin XAML中将转换器与Translate i18n一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在字符串末尾添加:".
I want to add ":" at the end of string.
public class StringToStringColonConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value + ":";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
如果我这样做,那就行了
If I do this it works
<Label Text="{Binding DocumentLabel, Converter={converter:StringToStringColonConverter}}" />
这种方式不起作用
<Label Text="{i18n:Translate some_text_value, Converter={converter:StringToStringColonConverter}}" />
我无法使用它.
推荐答案
您是否找到了解决方案?否则,我的解决方案可能会为您提供帮助. 我使用翻译扩展而不是i18n软件包
Did you find a solution for this? Otherwise my solution might help you. I use a translation extention instead of the i18n package
在扩展名中,将ResourceId设置为resx-File位置,添加自定义属性并在获得resourceManager的翻译文本后实现行为
in the extension set the ResourceId to your resx-File location add your custom property and implement the behavior after you got the translated text of the resourceManager
using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Project.Utils
{
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
const string ResourceId = "Project.Resources.AppResources";
public string Text { get; set; }
public IValueConverter Converter { get; set; }
public object ProvideValue(IServiceProvider serviceProvider)
{
if (Text == null)
return null;
ResourceManager resourceManager = new ResourceManager(ResourceId, typeof(TranslateExtension).GetTypeInfo().Assembly);
string translatedText = resourceManager.GetString(Text, CultureInfo.CurrentCulture);
if (this.Converter != null)
{
translatedText = Converter.Convert(translatedText, typeof(string), null, CultureInfo.CurrentCulture).ToString() ?? translatedText;
}
return translatedText;
}
}
}
然后您可以在XAML中设置转换器:
Then you can set the converter in the XAML:
xmlns:strings="clr-namespace:Project.Utils;assembly=Project"
<ContentPage.Resources>
<ResourceDictionary>
<converters:ColonSpaceConverter x:Key="ColonSpaceConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<Label Text="{strings:Translate Money, Converter={StaticResource ColonSpaceConverter}}" />
这篇关于在Xamarin XAML中将转换器与Translate i18n一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文