拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 系结来自mvvm的属性的文本时,标签中的换行符-StringFormat

系结来自mvvm的属性的文本时,标签中的换行符-StringFormat

白鹭 - 2022-03-07 1973 0 0

我将Label.FormattedTexton Labelfor ListViewsCell用于单个控制元件上的多个文本。我想要一个来自 mvvm 属性的换行符和文本。这是我想要格式化文本的方式

Text=" linebreak   property_text   | "

这段代码正在尝试,但在 xaml 中出现错误。

<Label FontSize="Medium" Text="{Binding name}" >
    <Label.FormattedText>
        <FormattedString>
            <Span Text="{Binding name}"/>
            <Span Text="{Binding balance, StringFormat='&#x0a; = {0:N}' | }" FontSize="Micro"/>
            <Span Text="Insufficiant balance" TextColor="Red" FontSize="Micro"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

它在这里显示了一些语法错误StringFormat='&#x0a; = {0:N}' | }

在我正在寻找的输出下方

系结来自 mvvm 的属性的文本时,标签中的换行符 - StringFormat

uj5u.com热心网友回复:

您必须首先在 xaml 上添加此命名空间:

xmlns:system="clr-namespace:System;assembly=netstandard"

然后在你的格式化文本中像这样使用它:

<Label FontSize="Medium">
    <Label.FormattedText>
        <FormattedString>
            <Span Text="{Binding Name}" />
            <Span Text="{Binding balance}" FontSize="Micro" />
            <Span Text=" | " />
            <Span Text="{x:Static system:Environment.NewLine"} />
            <Span Text="Insufficiant balance" TextColor="Red" FontSize="Micro"/>
        </FormattedString>
    </Label.FormattedText>
</Label>

uj5u.com热心网友回复:

您也可以添加自己的值转换器来执行此操作:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:MyBalanceConverter x:Key="balanceConv" />
    </ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
    <Label HorizontalOptions="Center"
           HorizontalTextAlignment="Start"
           VerticalOptions="Center">
        <Label.FormattedText>
            <FormattedString>
                <Span Text="{Binding CardName}" />
                <Span Text="{Binding Balance, Converter={StaticResource balanceConv}}"
                      FontSize="Micro" />
                <Span Text=" | "
                      FontSize="Micro" />
                <Span Text="Insufficient Funds"
                      TextColor="Red"
                      FontSize="Micro" />
            </FormattedString>
        </Label.FormattedText>
    </Label>
</ContentPage.Content>

IValueConverter定义转换的地方

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return string.Format("\n{0:C2}", (double)value);
}

这给你:

系结来自 mvvm 的属性的文本时,标签中的换行符 - StringFormat

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *