WPF 字符串资源中的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25545013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Line breaks in string resources
提问by liorda
In my WPF application I'm referencing strings from a centralized dictionary resource. How can I put line breaks in these strings?
在我的 WPF 应用程序中,我从一个集中的字典资源中引用字符串。如何在这些字符串中放置换行符?
I tried "line1\nline2", "line1\\nline2" and "line1
line2", but none are working.
我试过"line1\nline2", "line1\\nline2" and "line1
line2",但没有一个工作。
I should mention that I'm also including tokens in these strings ({0},...) and later using string.format(resource, args) in run-time.
我应该提到的是,我还在这些字符串 ({0},...) 中包含了标记,稍后在运行时使用 string.format(resource, args) 。
回答by liorda
Working solution: shift+enter in the dictionary resource window in visual studio seems to work.
工作解决方案:在 Visual Studio 的字典资源窗口中 shift+enter 似乎有效。
回答by IVAAAN123
Try to add xml:space="preserve"to your resource and use 
尝试添加xml:space="preserve"到您的资源并使用
<sys:String x:Key="MyString" xml:space="preserve">line1
line2</sys:String>
回答by codekaizen
Try the numeric character referenceapproach:
尝试数字字符参考方法:
<sys:String>line1 line2</sys:String>
Note, however, that if you are actually encoding an Inlineyou can use:
但是请注意,如果您实际上是在编码,则Inline可以使用:
<LineBreak />
E.g:
例如:
<TextBlock>
<TextBlock.Text>
line1 <LineBreak /> line2
</TextBlock.Text>
</TextBlock>
回答by Mayur Dhingra
If nothing works out, assured solution would be using a converter.
如果没有任何效果,有保证的解决方案将使用转换器。
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("

"))
s = s.Replace("

", Environment.NewLine);
if (s.Contains("
"))
s = s.Replace("
", Environment.NewLine);
if (s.Contains("
"))
s = s.Replace("
", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", 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();
}
}
回答by Milen Grigorov
The best and easy way if you are using the text editor for the resx file is by pressing
如果您对 resx 文件使用文本编辑器,最好和最简单的方法是按
Shift+Enter
Shift+Enter
for every new line that you want, as noted in other answers.
如其他答案中所述,对于您想要的每一行新行。
If you are using text from other source and you are copying/pasting the text in the resx, then the Visual Studio will paste the text in the same format as it already is, which will include the linebreaks.
如果您使用来自其他来源的文本并且您正在复制/粘贴 resx 中的文本,则 Visual Studio 将以与现有格式相同的格式粘贴文本,其中将包括换行符。

