简体   繁体   English

如何在Xamarin.Forms标签中添加换行符?

[英]How can I add a line feed into a Xamarin.Forms label?

Here is what I would like to do: 这是我想做的:

<Label Text="Choose By\nOne\nTwo" Margin="0,10,0,0" />

However the only thing this does is make the characters \\n appear twice in the label. 但是,唯一要做的就是使字符\\ n在标签中出现两次。 Is there any way that I can add a line feed in as part of a label's text? 有什么方法可以将换行符添加为标签文本的一部分?

You should use 你应该用

<Label Text="Choose By &#10;One &#10;Two" Margin="0,10,0,0" />

and the result will look like \\n 结果看起来像\\n

在此处输入图片说明

Replace the \\n by &#x0a; \\n替换为&#x0a; . Or set it in a more verbose mode like: 或者将其设置为更详细的模式,例如:

<Label Margin="0,10,0,0">
    <Label.Text>
        Choose By
        One
        Two
    </Label.Text>
</Label>

I just ran into this, but for me I was sending a string from a server to the device and needed a way to keep line feeds in the text. 我刚遇到这个问题,但是对我来说,我是从服务器向设备发送一个字符串,需要一种在文本中保留换行符的方法。 I ended up having to process the text on the device and find my special <THIS IS A LINE FEED> character and replace it with Environment.NewLine . 我最终不得不在设备上处理文本,找到我的特殊<THIS IS A LINE FEED>字符并将其替换为Environment.NewLine

So something like this: 所以像这样:

In your XAML: 在您的XAML中:

<Label Text="{Binding Something}" Margin="0,10,0,0" />

OR 要么

<Label x:Name="LabelMy" Margin="0,10,0,0" />

Then in code behind or ViewModel: 然后在后面的代码或ViewModel中:

string normalText = "Choose By\nOne\nTwo";

string fixedText = normalText.Replace("\n", Environment.NewLine);

ViewModel.Something = fixedText;

//OR

LabelMy.Text = fixedText;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM