如何在xamarin.forms中的xaml中大写/小写resx绑定?

bat*_*aci 2 resx xamarin xamarin.forms

遵循xamarin本地化主题和样本TodoLocalized,我使我的应用程序多语言.我的问题有时候,我需要使用有时单词大写,我不想在resx文件中创建另一个翻译作为同一个单词的大写版本.实现这一目标的最佳方法是什么?如果可能延长这个translateextension?或者我应该使用IValueConvertor?如果是,如何在xaml中绑定它

    // You exclude the 'Extension' suffix when using in Xaml markup
        [ContentProperty("Text")]
        public class TranslateExtension : IMarkupExtension
        {
            readonly CultureInfo ci;
            const string ResourceId = "myApp.Resx.AppRes";

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

            public string Text { get; set; }

            public object ProvideValue(IServiceProvider serviceProvider)
            {
                if (Text == null)
                    return "";

                ResourceManager resmgr = new ResourceManager(ResourceId
                                    , typeof(TranslateExtension).GetTypeInfo().Assembly);

                var translation = resmgr.GetString(Text, ci);

                if (translation == null)
                {
#if DEBUG
                    throw new ArgumentException(
                        String.Format("Key '{0}' was not found in resources '{1}' for culture '{2}'.", Text, ResourceId, ci.Name),
                        "Text");
#else
                translation = Text; // HACK: returns the key, which GETS DISPLAYED TO THE USER
#endif
                }
                return translation;
            }
        }
Run Code Online (Sandbox Code Playgroud)

我的Xaml:

     <Button  Image="ic_add.png"   Text="{resx:Translate AddNew}"
 Command="{Binding AddNew}"   HorizontalOptions="FillAndExpand"/>
Run Code Online (Sandbox Code Playgroud)

我试过这样做,但我猜Binding需要在BindingContext中定义一个属性.因此它不起作用,但我如何为resx文件中定义的文本实现它.

Text="{Binding {resx:Translate AddNew}, Converter={StaticResource UpperCaseConverter}}"
Run Code Online (Sandbox Code Playgroud)

Tim*_*äki 5

你可以尝试这样的事情:

<Button Text="{Binding Converter={StaticResource UpperCaseConverter}, ConverterParameter={resx:Translate AddNew}}"/>
Run Code Online (Sandbox Code Playgroud)

这样你就可以访问转换器中的翻译字符串

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
     return parameter.ToString().ToUpper();
}
Run Code Online (Sandbox Code Playgroud)

如果ConverterParameter不起作用,您可以只使用密钥并在转换器中获取已翻译的资源.