如何在FormattedString中添加新行

jit*_*ore 5 xamarin

 public FormattedString FormattedDescription
 {
     get
     {
         return new FormattedString
         {
            Spans = 
            {
                new Span 
                { 
                    Text = RoleName, 
                    FontSize = 16, 
                    FontAttributes = FontAttributes.Bold 
                },
                new Span 
                { 
                    Text = "/ " + ProjectRoleID + "/ "+Part + "/ "+Gender + "/ " + AgeRange
                },
           }
        };
    }
    set 
    { 
    }  
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我想在第一行显示RoleName,在第二行显示其他详细信息.

Dam*_*mer 45

这是一个完全在 XAML 中的“清洁”解决方案:

  • 首先,将此命名空间添加到您的 XAML: xmlns:system="clr-namespace:System;assembly=netstandard"

这是系统命名空间,它包含所有系统类。

  • 其次,在 XAML 中调用 Environment.NewLine,在您希望的 Span 中,如下所示:
<Label>
    <Label.FormattedText>
        <FormattedString>
            <Span Text="First Text"/>
            <Span Text=" "/>
            <Span Text="Second Text"/>
            <Span Text="{x:Static system:Environment.NewLine}"/>
            <Span Text="Above is a new line"/>
        </FormattedString>
    </Label.FormattedText>
</Label>
Run Code Online (Sandbox Code Playgroud)

在这里,您有一种干净的方式来显示新行。


Gio*_*rgi 8

您可以使用Environment.NewLine将文本移动到下一行,如下所示:

   public FormattedString FormattedDescription
   {
       get
       {
           return new FormattedString
           {
                Spans = {
                            new Span { Text = RoleName, FontSize = 16, FontAttributes = FontAttributes.Bold },
                            new Span { Text = Environment.NewLine, FontSize = 16 },
                            new Span { Text = "/ " + ProjectRoleID + "/ "+Part + "/ "+Gender + "/ " + AgeRange},
                        }
           };
       }
       set { }  
   }
Run Code Online (Sandbox Code Playgroud)


Mar*_*des 5

您还可以使用 XAML 属性元素语法来执行此操作:

 <Label>
    <Label.FormattedText>
        <FormattedString>
            <Span>
                <Span.Text>
Line 1
Line 2
                </Span.Text>
            </Span>
        </FormattedString>
    </Label.FormattedText>
</Label>
Run Code Online (Sandbox Code Playgroud)

  • 您可以在 XAML 中使用 \r\n,ASCII 代码为:CR &#13; 低频 带有 Replace 方法的自定义标签也可能有效。 (6认同)