Xamarin 表单标签新行
Xamarin forms Label new line
我的 Xamarin 表单页面中有一个 Label
,它从它的视图模型接收 Text
,并且文本包含 \n
字符。
我怎样才能让 Label
自动将文本分成两行?
可能是你的代码:
<Label FontSize="Medium" TextColor="Gray" Text="{translation:TranslationResource MyExpertsView_NoExpertsText}" IsVisible="{Binding IsNoExpertsFound}"></Label>
由于您的文本是从资源文件中检索的,Label
的 Text
属性 将输入视为纯文本,不允许任何转义字符。
您可以考虑预处理来自 TranslationResource
.
的字符串(将所有 \n 替换为换行符)
非常简单添加以下 unicode 而不是 \n
例如:
Text="General Studies - I"
我只使用 \n
,我正在使用 XAML 并且我通过标签上的 Binding
绑定文本。
最简单的方法是使用 "Shift+Enter" 在您的资源中添加新行
你可以这样做:
Text = "First line string
Second line string"
解决它的一种方法是在您的 VM 中将 '\n' 替换为 System.Environment.NewLine
string Text = "Text first line\nText second line".Replace("\n", System.Environment.NewLine);
要在 ViewModel 上更改它,您可以使用此方法。它会更新你的字符串上的所有 \n 作为 string.Replace 只是改变第一次出现。
// Replace \n to Environtment.NewLine
public string StringUpdateNewLineFormat(string sentences)
{
return Regex.Replace(sentences, @"^""|""$|\n?", System.Environment.NewLine);
}}
我的 Xamarin 表单页面中有一个 Label
,它从它的视图模型接收 Text
,并且文本包含 \n
字符。
我怎样才能让 Label
自动将文本分成两行?
可能是你的代码:
<Label FontSize="Medium" TextColor="Gray" Text="{translation:TranslationResource MyExpertsView_NoExpertsText}" IsVisible="{Binding IsNoExpertsFound}"></Label>
由于您的文本是从资源文件中检索的,Label
的 Text
属性 将输入视为纯文本,不允许任何转义字符。
您可以考虑预处理来自 TranslationResource
.
非常简单添加以下 unicode 而不是 \n
例如:
Text="General Studies - I"
我只使用 \n
,我正在使用 XAML 并且我通过标签上的 Binding
绑定文本。
最简单的方法是使用 "Shift+Enter" 在您的资源中添加新行
你可以这样做:
Text = "First line string
Second line string"
解决它的一种方法是在您的 VM 中将 '\n' 替换为 System.Environment.NewLine
string Text = "Text first line\nText second line".Replace("\n", System.Environment.NewLine);
要在 ViewModel 上更改它,您可以使用此方法。它会更新你的字符串上的所有 \n 作为 string.Replace 只是改变第一次出现。
// Replace \n to Environtment.NewLine
public string StringUpdateNewLineFormat(string sentences)
{
return Regex.Replace(sentences, @"^""|""$|\n?", System.Environment.NewLine);
}}