C# 表单WebAPI中的换行文本

C# 表单WebAPI中的换行文本,c#,json,xamarin,asp.net-web-api,xamarin.forms,C#,Json,Xamarin,Asp.net Web Api,Xamarin.forms,我的ASP.NET WebAPI项目正在公开一个端点: AboutLong属性中有大量文本,应该“分解”成几个段落 [ { "FirstName": "John", "LastName": "Doe", "FullName": "JohnDoe", "ImageUrl": "uriOfTheImage", "AboutShort": "CEO of AcmeCompany.", "AboutL

我的ASP.NET WebAPI项目正在公开一个端点: AboutLong属性中有大量文本,应该“分解”成几个段落

[
    {
        "FirstName": "John",
        "LastName": "Doe",
        "FullName": "JohnDoe",
        "ImageUrl": "uriOfTheImage",
        "AboutShort": "CEO of AcmeCompany.",
        "AboutLong": "Lorem Ipsum is simply dummy text of the printing and typesetting industry.\nLorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.\nIt has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.",
        "ContactInfo": null
    },
========================================================================
我想在我的SpeakerDetailPage.xaml上显示AboutLong:

<StackLayout Margin="16" Padding="0" Spacing="0">
        <Label Text="{Binding FullName}" FontSize="32" FontAttributes="Bold" HorizontalTextAlignment="Center" Margin="0,16,0,0" />
        <Label Text="{Binding AboutShort}" FontSize="22" HorizontalTextAlignment="Center" Margin="0,0,0,24" />
        <Label Text="{Binding AboutLong}" FontSize="16" />

========================================================================
当我手动输入X.F换行符时,它可以工作:

<Label Text="abcdefgh &#10; ijklmnop" />


但是,如果我只是绑定到模型(BindingContext被设置为从反序列化json字符串创建的模型),模拟器将显示文本以及所有换行符-\n
我还尝试用Xamarin.Forms换行符替换\n- 但随后它显示X.F线作为文本断开。

我们如何使API端点为客户端留下一条线索,即文本中应该有换行符?

问题是这些字符被处理为纯文本,因此您不会得到换行符。您应该创建一个ValueConverter,它接受长字符串并用
环境替换每个换行符。换行符
,如下所示:

public class TextBrakeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value as string).Replace("\\n", Environment.NewLine);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
XAML中的用法:

<ContentPage.Resources>
    <ResourceDictionary>
        <local:TextBrakeConverter x:Key="TextBrakeConverter" />
    </ResourceDictionary>
</ContentPage.Resources>

<Label Text="{Binding LongText, Converter={StaticResource TextBrakeConverter}}" />

我不知道这是怎么发生的(Xamarin.Forms更新了?),但突然间,文本中的“\n”是如何工作的。它打破了界限。

我没有改变任何事情。 我从服务器请求的API端点被反序列化为一个对象。该对象有一个AboutLong属性,其值为带有'\n'字符的长文本字符串。

标签文本属性绑定到对象的AboutLong属性。这一次,Xamarin读对了。

如果有人能向我解释这是怎么发生的,我将不胜感激。