简体   繁体   English

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

[英]How to use constant strings in C# designer.cs code?

How do I reference a constant string in my .designer.cs file? 如何在.designer.cs文件中引用常量字符串?

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. 直接的答案是在我的.cs文件中创建一个私有字符串变量,然后编辑designer.cs文件以使用此变量,而不是对字符串进行硬编码。 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? 我的每个UI控件都应该仅以文本作为占位符,然后仅在运行时覆盖所有文本属性吗? 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. 谢谢。

The *.designer.* files are generated by Visual Studio. *.designer.*文件由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. 除非确实需要,否则根本不需要手动修改它们,因为Visual Studio有时可能会重新生成它们并清除您的更改。

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. 有几种方法可以在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 . 如果按名称对属性进行排序,则在括号中的列表顶部将有两个属性: ApplicationSettingsDataBindings If you pull those down, you will see they both have options to bind to the Text property of your label. 如果将它们下拉,则会看到它们都具有绑定到标签的Text属性的选项。 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. 使用这些,您可以将“常量字符串”放入应用程序的设置文件中,并通过ApplicationSettings属性或任何数据源进行绑定,并通过DataBindings属性进行DataBindings Then the forms designer will generate the appropriate code for you. 然后,表单设计器将为您生成适当的代码。

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. 对我来说,要实现的目标还不是很清楚,但是通常,如果要避免将所有字符串都硬编码到设计器.cs文件中,则可以使用资源文件,该文件将在启动时从中加载。

If you set the "localizable" property of the form to be "true" I believe everything will end up in resources automatically. 如果您将表单的“ localizable”属性设置为“ true”,我相信一切都会自动归入资源。 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 :) 另一方面,我距离Windows Forms专家还很远,所以请捏一点盐,直到一切正常为止:)

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. 您将要在Page_Init事件方法中进行控件更改。 This may allow, your changes to show up in the designer. 这样可以使您的更改显示在设计器中。

http://msdn.microsoft.com/en-us/library/system.web.ui.control.init.aspx http://msdn.microsoft.com/zh-CN/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. 更新:抱歉没有意识到这是WinForms。 It is actually even easier in WinForms, because you just register your controls in the constructor right after the control initialization is called. 实际上,它在WinForms中甚至更加容易,因为您只需在调用控件初始化之后立即在构造函数中注册控件。

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. 如果您尝试实现本地化,请查看.NET和卫星组件随附的本地化功能。

If you're trying to solve customization, look at the customization options available in .NET, with the settings editor. 如果要解决自定义问题,请使用设置编辑器查看.NET中可用的自定义选项。

Why do you need to use constant strings? 为什么需要使用常量字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM