【发布时间】:2018-12-20 06:18:42
【问题描述】:
如何添加:在 xamarin.forms 中的 xaml 中的标签处添加字符串。我有一个来自应用程序资源文件的文本(i18n:Translate Text=Supplier)。现在有了这段文字,我还添加了:在这段文字之后。我不想在带有文本的应用程序资源中添加 :。我只想在 xaml 上这样做。我尝试过使用 StringFormat,但不知道该怎么做。
【问题讨论】:
标签: xaml xamarin xamarin.ios
如何添加:在 xamarin.forms 中的 xaml 中的标签处添加字符串。我有一个来自应用程序资源文件的文本(i18n:Translate Text=Supplier)。现在有了这段文字,我还添加了:在这段文字之后。我不想在带有文本的应用程序资源中添加 :。我只想在 xaml 上这样做。我尝试过使用 StringFormat,但不知道该怎么做。
【问题讨论】:
标签: xaml xamarin xamarin.ios
这可以通过多种方式实现,草拟其中两个:(Label的值将在滑动Slider时更改)
方法一
<Label Text="Slide to change Value"
VerticalOptions="CenterAndExpand"
HorizontalOptions="CenterAndExpand"
x:Name="lblSliderValue" FontSize="Title" Margin="60"></Label>
<Slider ValueChanged="Slider_ValueChanged"></Slider>
在 CodeBehind 文件中,
private void Slider_ValueChanged(object sender, ValueChangedEventArgs e)
{
lblSliderValue.Text = e.NewValue.ToString("0.00");
lblSliderValue.BackgroundColor = Color.Black;
lblSliderValue.TextColor = Color.White;
}
方法二(CodeBehind文件中不需要代码)
<Label Text="Slide to change Value"
VerticalOptions="Center"
HorizontalOptions="Center"
Text="{Binding Source={x:Reference sldExample}, Path=Value, StringFormat='{0:F2}'}"></Label>
<Slider x:Name="sldExample" BackgroundColor="Yellow" ThumbColor="Violet"></Slider>
【讨论】:
您可以使用Value Converter 更改绑定上的值,但这很棘手,因为您在使用 i18n:Translate 时无法轻松添加转换器。但我仍然看到您的问题的三种可能解决方案:
最简单的方法是创建一个获取翻译文本的属性,然后在文本中添加一个冒号:
视图模型:
public string Supplier
{
get { return AppResources.Supplier + ":"; }
}
XAML:
<Label Text="{Binding Supplier}"/>
另一种方法是创建一个获取翻译文本的属性,然后通过值转换器添加冒号:
视图模型:
public string Supplier
{
get { return AppResources.Supplier; }
}
转换器类:
public class ColonConverter : 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 value.ToString().Remove(value.ToString().Length - 1);
}
}
XAML:
<ContentPage.Resources>
<ResourceDictionary>
<local:ColonConverter x:Key="ColonConverter" />
</ResourceDictionary>
</ContentPage.Resources>
...
<Label Text={Binding Supplier, Converter={StaticResource ColonConverter}}"/>
3.创建您自己的翻译扩展和值转换器
我没有对此进行测试,但我找到了this SO answer,它提供了一个关于如何实现这一目标的示例。这样您就不需要向 ViewModel 添加属性,因此您只需在设置翻译扩展和转换器后调整您的 XAML。但是编写自己的翻译扩展需要一些工作。
自定义翻译扩展:
[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:
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}}" />
【讨论】: