如何在 C# Designer.cs 代码中使用常量字符串?

发布于 2024-07-19 09:08:02 字数 282 浏览 2 评论 0原文

如何在 .designer.cs 文件中引用常量字符串?

一个直接的答案是在我的 .cs 文件中创建一个私有字符串变量,然后编辑 Designer.cs 文件以使用此变量,而不是对字符串进行硬编码。 但设计者不喜欢这样抛出错误。 我明白为什么这行不通,但我不确定最好的选择是什么。

如果我的每个 UI 控件都只有文本作为占位符,那么我只需要在运行时重写所有文本属性吗? 这样我就失去了在设计器中看到一切的好处。

我只是想弄清楚当未来事情发生变化时如何造成最少的干扰。 谢谢。

How do I reference a constant string in my .designer.cs file?

A straight forward answer is to create a private string variable in my .cs file, then edit the designer.cs file to use this variable instead of hardcoding the string. But the designer doesn't like this throws an error. I understand why this can't work, but I'm not sure of what the best alternative is.

Should each of my UI controls just have text as a place holder, then I need to override all text properties only at runtime? This way I loose the benefit of seeing everything in the designer.

I'm just trying to figure how to cause the least disruption when things change in the future. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丑丑阿 2024-07-26 09:08:02

*.designer.* 文件由 Visual Studio 生成。 除非确实需要,否则您根本不想手动修改它们,因为 Visual Studio 有时可能重新生成它们并删除您的更改。

您真正需要做的是将常量字符串视为您希望表单设计器可以使用的资源,以便在生成代码时使用该资源。

在 Visual Studio 中,有多种方法可以实现此目的。 最简单的可能是通过控件的数据绑定。 为了进行演示,请将标签控件放在窗体上并查看该控件的属性。 如果按名称对属性进行排序,列表顶部将有两个属性用括号括起来:ApplicationSettingsDataBindings。 如果将它们下拉,您将看到它们都有绑定到标签的 Text 属性的选项。 使用这些,您可以将“常量字符串”放入应用的设置文件中并通过 ApplicationSettings 属性绑定它,或者放入任何数据源中并通过 DataBindings 属性绑定它。 然后表单设计器将为您生成适当的代码。

The *.designer.* files are generated by Visual Studio. You don't want to modify them by hand at all unless you really have to, because Visual Studio may at times re-generate them and erase your changes.

What you really need to do is think of your constant string a resource that you want to be available to the forms designer, so that when it generates the code it uses that resource.

There are several ways to accomplish this in Visual Studio. The easiest is probably via databindings for the controls. To demonstrate, put a label control on a form and look at the properties for the control. If you sort the properties by name there will be two properties at the top of the list enclosed in parentheses: ApplicationSettings and DataBindings. If you pull those down, you will see they both have options to bind to the Text property of your label. Using these, you can put your "constant string" in either your app's settings file and bind it via the ApplicationSettings property or in any datasource and bind it via the DataBindings property. Then the forms designer will generate the appropriate code for you.

太傻旳人生 2024-07-26 09:08:02

我并不完全清楚您想要实现的目标,但通常如果您想避免将字符串全部硬编码到设计器 .cs 文件中,您可以使用一个资源文件,在启动时将从该资源文件加载字符串。

如果您将表单的“可本地化”属性设置为“true”,我相信所有内容都会自动出现在资源中。 另一方面,我远非 Windows 窗体专家,因此在您开始使用之前,请对这一切持保留态度:)

It's not entirely clear to me what you're trying to achieve, but normally if you want to avoid the strings all being hard-coded into the designer .cs file, you use a resource file which the strings will be loaded from on startup.

If you set the "localizable" property of the form to be "true" I believe everything will end up in resources automatically. On the other hand, I'm far from expert at Windows Forms, so take all this with a pinch of salt until you've got it working :)

揽清风入怀 2024-07-26 09:08:02

你的第二种方法是最好的选择。 因为你不能假设设计师总是会保留你的更改,而且很多时候它只会把它们吹走。 您将需要在 Page_Init 事件方法中进行控件更改。 这可能允许您的更改显示在设计器中。

http://msdn.microsoft.com/ en-us/library/system.web.ui.control.init.aspx

这样,您就可以在您控制的实际代码隐藏类中保留常量,并且控件将以您想要的方式启动,并且你可以控制你想要改变的事情。

更新:抱歉,我没有意识到这是WinForms。 实际上,在 WinForms 中甚至更容易,因为您只需在调用控件初始化后立即在构造函数中注册控件即可。

Your second method is the best bet. Because you can't assume the designer is always going to keep your changes around, and often times it just blows them away. You are going to want to do your control changes in Page_Init event method. This may allow, your changes to show up in the designer.

http://msdn.microsoft.com/en-us/library/system.web.ui.control.init.aspx

This way you get to keep you constants in your actual code-behind class that you control and the controls will be initiated the way you want and you can just control the things you want to change.

Update: sorry didn't realize this was WinForms. It is actually even easier in WinForms, because you just register your controls in the constructor right after the control initialization is called.

青瓷清茶倾城歌 2024-07-26 09:08:02

你想解决什么问题? 如果您尝试实现本地化,请查看 .NET 和卫星程序集附带的本地化功能。

如果您尝试解决自定义问题,请使用设置编辑器查看 .NET 中提供的自定义选项。

为什么需要使用常量字符串?

What problem are you trying to solve? If you're trying to implement localization, look at the localization features that come with .NET and with sattelite assemblies.

If you're trying to solve customization, look at the customization options available in .NET, with the settings editor.

Why do you need to use constant strings?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文