xamarin C#多个绑定标签与字符串和数据

rseugnpd  于 7个月前  发布在  C#
关注(0)|答案(5)|浏览(72)

快速提问:
我如何绑定一个标签与固定字符串本身?像“ID:xxxxx”“ID:“是固定字符串和xxxx是数据的唯一方法,我可以使它是创建两个标签在UI中。
有没有什么方法可以用一个标签来做?

waxmsbnn

waxmsbnn1#

你可以直接在Xaml中这样做:

<Label Text="{Binding Id, StringFormat='ID:{0}'}}"/>

字符串
在视图模型中不需要转换器或其他属性;)

tquggr8v

tquggr8v2#

在过去的项目中,我使用多个可绑定的值属性创建了自己的自定义标签类,这些属性只是将值连接在一起。

public class MultiLabel : Label
{
    public static readonly BindableProperty Value1Property = BindableProperty.Create(nameof(Value1), typeof(string), typeof(MultiLabel), string.Empty, propertyChanged: OnValueChanged);
    public static readonly BindableProperty Value2Property = BindableProperty.Create(nameof(Value2), typeof(string), typeof(MultiLabel), string.Empty, propertyChanged: OnValueChanged);
    public static readonly BindableProperty Value3Property = BindableProperty.Create(nameof(Value3), typeof(string), typeof(MultiLabel), string.Empty, propertyChanged: OnValueChanged);
    public static readonly BindableProperty Value4Property = BindableProperty.Create(nameof(Value4), typeof(string), typeof(MultiLabel), string.Empty, propertyChanged: OnValueChanged);
    public static readonly BindableProperty Value5Property = BindableProperty.Create(nameof(Value5), typeof(string), typeof(MultiLabel), string.Empty, propertyChanged: OnValueChanged);

    public string Value1
    {
        get { return (string)GetValue(Value1Property); }
        set { SetValue(Value1Property, value); }
    }

    public string Value2
    {
        get { return (string)GetValue(Value2Property); }
        set { SetValue(Value2Property, value); }
    }

    public string Value3
    {
        get { return (string)GetValue(Value3Property); }
        set { SetValue(Value3Property, value); }
    }

    public string Value4
    {
        get { return (string)GetValue(Value4Property); }
        set { SetValue(Value4Property, value); }
    }

    public string Value5
    {
        get { return (string)GetValue(Value5Property); }
        set { SetValue(Value5Property, value); }
    }

    static void OnValueChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var label = (MultiLabel)bindable;

        label.Text = label.Value1 + label.Value2 + label.Value3 + label.Value4 + label.Value5;
    }
}

字符串
在xaml中,

<local:MultiLabel Value1="ID:" Value2="{Binding Id}" />


我们这样做的主要原因是减少标签的数量以减少内存占用,并且您所需要的值的数量总是可以更改的,只是碰巧我们最终需要的最大值是5。

crcmnpdw

crcmnpdw3#

一种方法是创建一个新的属性来输出您的自定义格式,例如:

public string MyCustomFormatProperty{
    get {
        return string.Format("ID:{0}",  this.XXX);
    }
}

<Label Text = "{Binding MyCustomFormatProperty}" />

字符串
另一种方法是使用转换器:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/data-binding/converters

kkih6yb8

kkih6yb84#

你可以连接字符串与固定标签字符串
例如:Lable1.Text=Lable1.Text+“xxxxxxx”;
我想这就是你的问题所需要的。

kyvafyod

kyvafyod5#

通过使用FormattedString设置Label.FormattedText来使用由多个文本元素组成的Label,称为Span。每个Span.Text也可以通过绑定进行设置。您甚至可以单独格式化每个Span
有关详细信息,请访问https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/text/label#formatted-text。
举个例子:

<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="ID:" />
            <Span Text="{Binding ID}" />
        </FormattedString>
    </Label.FormattedText>
</Label>

字符串

相关问题