我的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 ijklmnop" />
但是,如果我只是绑定到模型(BindingContext设置为从反序列化的json字符串创建的模型),模拟器将显示文本以及所有换行符 - \ n
我也尝试用Xamarin.Forms换行替换\ n -
但随后它显示X.F换行符为文本。
我们如何让API端点为客户提供文本中应该有换行符的线索?
答案 0 :(得分:1)
问题是那些\ n字符是以纯文本形式处理的,所以你不会得到换行符。您应该创建一个ValueConverter,它接受长字符串并用Environment.NewLine
替换每个换行符,如下所示:
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}}" />
答案 1 :(得分:0)
我不知道这是怎么发生的(Xamarin.Forms更新了吗?),但突然之间'\ n'在文本中的工作方式如何。它打破了线条。
我没有改变什么。
我从服务器请求的API端点被反序列化为对象。该对象具有AboutLong属性,其值为包含'\ n'个字符的长字符串。
标签Text属性绑定到对象的AboutLong属性。这次Xamarin正确地读了它。
如果有人能向我解释这是怎么发生的,我将感激不尽。