我正在创建一个Windows Phone应用程序,我发现自己多次编写相同的MessageBox.Show("Same error message")
。例如
"Could not connect to server"
当用户无法访问互联网时会发生这种情况。
是否可以将其放在某个地方,以便我编写一次文本并在各处获取相同的文本?
我可以编写一个静态类,但也许有一个文件?
最佳答案
Is there somewhere I can put it so that I write the text once and fetch the same text all over the place?
是的,有一种专门用于此目的的特殊文件,称为strings.resx
。它可以让你写
MessageBox.Show(strings.ServerNotFound);
而不是
MessageBox.Show("Server not found");
使用 strings.resx
的额外好处(事实上,预期目的)是您的应用程序可以轻松本地化 ( see answer to this question ):添加正确的翻译并设置当前区域设置即可将需要更改应用程序向用户显示的所有字符串以及正确的本地翻译。
关于c# - 我可以在哪里放置静态字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14797609/