gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:05:27 26 4
gpt4 key购买 nike

简短的问题:我的资源中有一个字符串:“这是我的测试字符串 {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") 不会改变任何东西。

Edit2:在我的资源文件中使用 Shift+Enter 而不是\n 似乎可以工作...无论如何都是奇怪的行为

最佳答案

Put a place holder where you want to put new line and in the code where you use that resource string, just replace it with new line: string resource: "This is first line.{0}This is second line.{0}This is third line." You will use this resource string like this: MessageBox.Show(string.Format(MyStringResourceClass.MyStringPropertyName, Environment.NewLine));

非常规方法 但是我现在通过直接(或任何其他地方)从 word 处理换行符并将其粘贴到资源字符串文件中来让它工作。

It was simple..
OR

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

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

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

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");

关于c# - 新行在 C#/WPF 中的 MessageBox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22402452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com