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

o2gm4chl  于 11个月前  发布在  C#
关注(0)|答案(5)|浏览(133)

简短问题:我的资源中有一个字符串:“这是我的测试字符串{0}\n\nTest”
我正在尝试在我的消息框中显示此字符串:

MessageBox.Show(String.Format(Properties.Resources.About, 
    Constants.VERSION), 
    Properties.Resources.About_Title, MessageBoxButton.OK, 
    MessageBoxImage.Information);

字符串
但是我没有得到新的行。\n仍然显示为字符,而不是新的行。
我还尝试使用了一个类似mystring.Replace(“\n”,Environment.NewLine)的解决方法,但这也没有改变任何东西。
我做错了什么?
编辑:有趣的是:Replace(“\n”,“somethingelse”)不会改变任何东西。
编辑2:Shift+Enter在我的资源文件中,而不是\n似乎工作...奇怪的行为无论如何

1rhkuytd

1rhkuytd1#

在要放置新行的位置放置一个占位符保持器,并在使用该资源字符串的代码中将其替换为新行:string resource:“This is first line.{0}This is second line.{0}This is third line.”您将像这样使用此资源字符串:MessageBox.Show(string.Format(MyStringResourceClass.MyStringPropertyName,Environment.NewLine));

非常规的方法,但我刚刚得到了它的工作,从Word直接复制换行符(或任何其他地方)和粘贴它在资源字符串文件。

It was simple..
OR

字符串
\r\n当您使用消息框显示字符或将其分配给文本框时,或当您在界面中使用它时,字符将转换为新行。
在C#中(像大多数C派生语言一样),转义字符用于表示特殊字符,如return和tab,而+用于代替&进行字符串连接。
要让你的代码在C#下工作,你有两个选择..

MessageBox.Show("this is first line" + "\n" + "this is second line");


另一种方法,更正确的是将其替换为Environment.NewLine,理论上它可以根据您使用的系统而改变(尽管不太可能)。

MessageBox.Show("this is first line" + Environment.NewLine + "this is second line");

a5g8bdjr

a5g8bdjr2#

在资源编辑器中使用shift+enter分隔字符串内容。或者,在xml编辑器中编辑ResX文件,然后使用enter键为资源字符串创建一个新行。
详细信息请参考此链接:Carriage Return/Line in ResX file.

nbysray5

nbysray53#

试试这个:

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

字符串
另一个变量的例子:

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

7gs2gvoe

7gs2gvoe4#

简单的解决方案:在资源设计器中:使用center +Enter在字符串中插入一个新行。

6tqwzwtp

6tqwzwtp5#

试试这个
删除MessageBoxImage.OK和MessageBoxImage.信息。

MessageBox.Show(String.Format(Properties.Resources.About, 
Constants.VERSION), 
Properties.Resources.About_Title);

字符串

相关问题