![](/img/trans.png)
[英]In Xamarin.Forms, how to allow for autocompletion for bindings in XAML code in the case you can't instantiate the BindingContext from XAML?
[英]how to upper/lower case resx bindings in xaml in 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;
}
}
我的 Xml:
<Button Image="ic_add.png" Text="{resx:Translate AddNew}"
Command="{Binding AddNew}" HorizontalOptions="FillAndExpand"/>
我嘗試這樣做,但我猜 Binding 需要在 BindingContext 中定義一個屬性。 因此它不起作用,但我如何為 resx 文件中定義的文本實現它。
Text="{Binding {resx:Translate AddNew}, Converter={StaticResource UpperCaseConverter}}"
你可以嘗試這樣的事情:
<Button Text="{Binding Converter={StaticResource UpperCaseConverter}, ConverterParameter={resx:Translate AddNew}}"/>
這樣你應該能夠訪問轉換器中的翻譯字符串
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return parameter.ToString().ToUpper();
}
如果ConverterParameter
不起作用,您可以只使用密鑰並在轉換器內獲取已翻譯的資源。
hankide 的回答也是正確的,但是我在這里找到了更好的解決方案。 顯然,在 IMarkupExtension 繼承類中定義的任何公共屬性都可以綁定。 參考xamarin 文檔中的示例
所以我只是擴展了我的 TranslationExtension 類
public bool IsUpper { get; set; } = false;
並將其綁定到我需要大寫的地方。 我希望這可以幫助任何正在尋找相同解決方案的人。
<Button Image="ic_add.png" Text="{resx:Translate AddNew}"
Command="{Binding AddNew, IsUpper=true}" HorizontalOptions="FillAndExpand"/>
為了使其更加可定制,您可以在 TranslationExtension 中添加一個屬性 StringCase,如下所示:
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
public string Text { get; set; }
public string StringCase { get; set; }
........
........
if (!string.IsNullOrEmpty(StringCase))
{
if (StringCase.ToUpper().Equals("UPPER"))
{
translation = translation.ToUpper();
}
else if (StringCase.ToUpper().Equals("LOWER"))
{
translation = translation.ToLower();
}
}
return translation;
}
}
保持文本正常:
<Label Text="{resx:Translate AddNew}"/>
<Button Text="{resx:Translate AddNew}"/>
使其成為大寫:
<Label Text="{resx:Translate AddNew, StringCase=Upper}"/>
<Button Text="{resx:Translate AddNew, StringCase=Upper}"/>
使其成為小寫:
<Label Text="{resx:Translate AddNew, StringCase=Lower}"/>
<Button Text="{resx:Translate AddNew, StringCase=Lower}"/>
這樣,如果您想將其轉換為 CamelCase、PascalCase、SnakeCase 或 KebabCase,您也可以添加條件(但是,除了大寫或小寫之外,您可能不會遇到需要轉換的情況)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.