Xaml 如何添加:使用动态绑定文本值的标签?

Xaml 如何添加:使用动态绑定文本值的标签?,xaml,xamarin,xamarin.ios,Xaml,Xamarin,Xamarin.ios,如何添加:在xamarin.forms中的xaml标签处添加字符串。我有一个来自app资源文件的文本(i18n:Translate text=Supplier)。现在,在这段文字之后,我还要加上:在这段文字之后。我不想添加:带文本的应用内资源。我只想在xaml上这样做。我试过使用StringFormat,但不知道如何操作。您可以使用更改绑定上的值,但这很棘手,因为在使用i18n:Translate时无法轻松添加转换器。但我仍然看到三种可能的解决方案: 1.不带值转换器的属性 最简单的方法是创建一

如何添加:在xamarin.forms中的xaml标签处添加字符串。我有一个来自app资源文件的文本(i18n:Translate text=Supplier)。现在,在这段文字之后,我还要加上:在这段文字之后。我不想添加:带文本的应用内资源。我只想在xaml上这样做。我试过使用StringFormat,但不知道如何操作。

您可以使用更改绑定上的值,但这很棘手,因为在使用i18n:Translate时无法轻松添加转换器。但我仍然看到三种可能的解决方案:

1.不带值转换器的属性 最简单的方法是创建一个属性,获取翻译后的文本,然后在文本中添加冒号:

视图模型:

public string Supplier
{
    get { return AppResources.Supplier + ":"; }
}
<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);
    }
}
<ContentPage.Resources>
    <ResourceDictionary>
        <local:ColonConverter x:Key="ColonConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

...

<Label Text={Binding Supplier, Converter={StaticResource ColonConverter}}"/>
[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;
        }
    }
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}}" />
XAML:

public string Supplier
{
    get { return AppResources.Supplier + ":"; }
}
<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);
    }
}
<ContentPage.Resources>
    <ResourceDictionary>
        <local:ColonConverter x:Key="ColonConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

...

<Label Text={Binding Supplier, Converter={StaticResource ColonConverter}}"/>
[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;
        }
    }
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}}" />
转换器类别:

public string Supplier
{
    get { return AppResources.Supplier + ":"; }
}
<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);
    }
}
<ContentPage.Resources>
    <ResourceDictionary>
        <local:ColonConverter x:Key="ColonConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

...

<Label Text={Binding Supplier, Converter={StaticResource ColonConverter}}"/>
[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;
        }
    }
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}}" />
XAML:

public string Supplier
{
    get { return AppResources.Supplier + ":"; }
}
<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);
    }
}
<ContentPage.Resources>
    <ResourceDictionary>
        <local:ColonConverter x:Key="ColonConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

...

<Label Text={Binding Supplier, Converter={StaticResource ColonConverter}}"/>
[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;
        }
    }
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}}" />
XAML:

public string Supplier
{
    get { return AppResources.Supplier + ":"; }
}
<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);
    }
}
<ContentPage.Resources>
    <ResourceDictionary>
        <local:ColonConverter x:Key="ColonConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

...

<Label Text={Binding Supplier, Converter={StaticResource ColonConverter}}"/>
[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;
        }
    }
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}}" />
xmlns:strings=“clr命名空间:Project.Utils;assembly=Project”

这可以通过多种方式实现,其中两种方式是:(滑动滑块时标签的值将发生变化)

方法1

<Label Text="Slide to change Value" 
                   VerticalOptions="CenterAndExpand"
                   HorizontalOptions="CenterAndExpand"
                   x:Name="lblSliderValue" FontSize="Title" Margin="60"></Label>
<Slider ValueChanged="Slider_ValueChanged"></Slider>

方法2(不需要代码隐藏文件中的代码)