新行在C#/ WPF中的MessageBox中不起作用

Fra*_*e91 17 c# wpf messagebox

简短的问题:我的资源中有一个字符串:"这是我的测试字符串{0} \n \nTest"

我正在尝试在我的Messagebox中显示此字符串:

MessageBox.Show(String.Format(Properties.Resources.About, 
    Constants.VERSION), 
    Properties.Resources.About_Title, MessageBoxButton.OK, 
    MessageBoxImage.Information);
Run Code Online (Sandbox Code Playgroud)

但是我没有得到新的台词.\n仍然显示为字符,而不是新行.

我也尝试使用像mystring.Replace("\n",Environment.NewLine)这样的解决方法,但这也没有改变任何东西.

我究竟做错了什么?

编辑:有趣的事情提到:替换("\n","somethingelse")不会改变任何东西.

编辑2:Shift + Enter在我的资源文件而不是\n似乎工作...无论如何奇怪的行为

Arj*_*ary 24

将占位符放在要放置新行的位置,并在使用该资源字符串的代码中,只需将其替换为新行:string resource:"这是第一行.{0}这是第二行.{0}这是第三行." 您将使用此资源字符串,如下所示:MessageBox.Show(string.Format(MyStringResourceClass.MyStringPropertyName,Environment.NewLine));

要么

非常规方法但是我刚刚通过直接(或任何其他地方)处理新行并将其粘贴到资源字符串文件中来使其工作.

It was simple..
OR
Run Code Online (Sandbox Code Playgroud)

\ r \n当您使用消息框显示字符或将其分配到文本框或在界面中使用时,字符将转换为新行.

在C#中(与大多数C派生语言一样),转义字符用于表示特殊字符,例如return和tab,而+用于代替&用于字符串连接.

为了使你的代码在C#下工作,你有两个选择......第一个是简单地用返回转义字符替换NewLine \n ala:

MessageBox.Show("this is first line" + "\n" + "this is second line");
Run Code Online (Sandbox Code Playgroud)

另一种方法,更正确的是用Environment.NewLine替换它,理论上它可能会根据你使用的系统而改变(不太可能).

MessageBox.Show("this is first line" + Environment.NewLine + "this is second line");
Run Code Online (Sandbox Code Playgroud)


pet*_*jan 7

在资源编辑器中,使用shift + enter分隔您的字符串内容.或者,在xml编辑器中编辑ResX文件,然后使用回车键为资源字符串创建一个新行.

有关详细信息,请参阅此链接:ResX文件中的回车符/行.


Chr*_*ris 6

尝试这个:

    String outputMessage = string.Format("Line 1{0}Line 2{0}Line 3", Environment.NewLine);
    MessageBox.Show(outputMessage);
Run Code Online (Sandbox Code Playgroud)

另一个带有另一个变量的示例:

    String anotherValue = "Line 4";
    String outputMessage = string.Format("Line 1{0}Line 2{0}Line 3{0}{1}", Environment.NewLine, anotherValue);
    MessageBox.Show(outputMessage);
Run Code Online (Sandbox Code Playgroud)