WPF字符串资源中的换行符

14

在我的WPF应用程序中,我正在从集中的字典资源引用字符串。如何在这些字符串中插入换行符?

我尝试过 "line1\nline2"、"line1\\nline2" 和 "line1
line2",但都没有起作用。

需要注意的是,我还在这些字符串中包含标记({0}等),并且稍后在运行时使用string.format(resource, args)。


如果你需要换行,为什么不将字符串拆分为两个不同的字符串? - Juan
2
你应该在ResourceDictionary或单个资源中添加xml:space="preserve" - marbel82
6个回答

23

解决方案:在Visual Studio中的字典资源窗口中使用shift+enter似乎可以起作用。


13

尝试将xml:space = "preserve"添加到您的资源中,并使用

<sys:String x:Key="MyString" xml:space="preserve">line1&#13;line2</sys:String>

4
在我的测试中,"xml:space="preserve"" 会导致其断开。另外请注意,您忘记了分号。 - codekaizen
2
是的,你忘了分号。应该是 - nikhil
2022年更新 .net4.8:添加 xml:space="preserve" + 只需复制粘贴原始内容即可使其正常工作。 - Glitch

5
尝试使用“数字字符引用”方法:

请参考XML和HTML字符实体引用列表

<sys:String>line1&#13;line2</sys:String>

请注意,如果您实际上正在编码一个内联,您可以使用以下方法:
<LineBreak />

E.g:

<TextBlock>
    <TextBlock.Text>
         line1 <LineBreak /> line2
    </TextBlock.Text>
</TextBlock>

请注意,十六进制字面量需要 xml:space="preserve" - fadden
当我测试时,xml:space="preserve"破坏了它。 - codekaizen
奇怪。我正在使用 Application.Current.FindResource() 获取字符串资源,并将其传递给 MessageBox.Show(),以呈现多行错误消息。如果没有使用 preserve,它就会变成一行。(示例) - fadden
“*尝试使用十六进制字面量[...] *”看起来很像十进制,但不太符合字面意思。 - mins

2
如果一切都不起作用,保证的解决方案是使用转换器。
public class NewLineConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = string.Empty;

        if (value.IsNotNull())
        {
            s = value.ToString();

            if (s.Contains("\\r\\n"))
                s = s.Replace("\\r\\n", Environment.NewLine);

            if (s.Contains("\\n"))
                s = s.Replace("\\n", Environment.NewLine);

            if (s.Contains("&#x0a;&#x0d;"))
                s = s.Replace("&#x0a;&#x0d;", Environment.NewLine);

            if (s.Contains("&#x0a;"))
                s = s.Replace("&#x0a;", Environment.NewLine);

            if (s.Contains("&#x0d;"))
                s = s.Replace("&#x0d;", Environment.NewLine);

            if (s.Contains("&#10;&#13;"))
                s = s.Replace("&#10;&#13;", Environment.NewLine);

            if (s.Contains("&#10;"))
                s = s.Replace("&#10;", Environment.NewLine);

            if (s.Contains("&#13;"))
                s = s.Replace("&#13;", Environment.NewLine);

            if (s.Contains("<br />"))
                s = s.Replace("<br />", Environment.NewLine);

            if (s.Contains("<LineBreak />"))
                s = s.Replace("<LineBreak />", Environment.NewLine);
        }

        return s;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

2

如果您正在使用文本编辑器处理resx文件,最好且最容易的方法是在需要新起一行时按下

Shift+Enter

这与其他答案中所述相同。

如果您从其他来源复制/粘贴文本到resx中,则Visual Studio会将文本以与原格式相同的方式粘贴,其中包括换行符。


1
我没有足够的声望来进行评论,所以我将补充IVAAAN123的答案,这对我来说很好用(带分号),但是,如果您在文本中添加任何换行符(使其更易读),请确保XML中没有制表符,因此新行从页面边缘开始,而不是制表符插入页面,否则“xml:space =”preserve“”将包括每行开头的空格:
<system:String x:Key="KeyName" xml:space="preserve">
    First line of text.&#13;
    Second line of text.
</system:String>

该命令将显示每行前面的所有空格。它还会显示两个额外的换行符:第一行之前和在文本中使用物理换行符后。去除空格和额外的换行符即可正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接