在设计器生成的代码中使用 #if 指令与 Windows 窗体进行条件编译
背景:
我有一个 C# Windows 窗体应用程序,其中包含一个 Windows 服务和一个用于配置系统设置以及与该服务通信的界面。
期望结果:
我想构建两个版本的解决方案 - 一个客户端版本,其中包含所有 Windows 服务相关代码和表单元素,以及一个服务器版本> 包含所有内容的版本。
该表单包含一个选项卡式控件,其中一个选项卡包含用于使用套接字与 Windows 服务进行交互的元素。我真正想要实现的是,对于完整构建,编译包含服务相关元素的选项卡,而对于条件构建,排除相同的选项卡。
问题:
在此阶段,我在 Windows 服务相关代码中使用了 #if 指令。例如:
#if SERVERBUILD
//Code relating to Windows service that I do not want to compile
//for a client version.
#endif
在上面的示例中,“SERVERBUILD”对应于我可以通过配置管理器选择的构建配置(而不是标准的“Release”构建选项)。
我遇到的问题是,我必须包装此 #if 指令的一些代码位于标题为“Windows 窗体设计器生成的代码”区域中的 WinForm.Designer.cs 文件中。
似乎发生的情况是,当我更改某些表单属性时,整个代码区域似乎被删除并重新生成,从而删除了我添加的 #if 部分。
- 我以正确的方式处理这件事吗?
- 有没有办法避免丢失在 WinForm.Designer.cs 代码中所做的更改的情况?
我真的很感激任何有条件编译和此类内容经验的人提供的建议。
Background:
I have a C# Windows Forms application that contains a Windows service and an interface used to configure system settings as well as communicate with the service.
Desired Outcome:
I would like to build two versions of the solution - a client version with all the Windows service related code and form elements and a server version that contains everything.
The form contains a tabbed control, where one tab contains elements used to interface with the Windows service using sockets. All I'm really trying to achieve is that for a full build the tab containing service related elements is compiled, while for a conditional build the same tab is excluded.
Problem:
At this stage I've used #if directives around the Windows service related code. For example:
#if SERVERBUILD
//Code relating to Windows service that I do not want to compile
//for a client version.
#endif
In the above example, 'SERVERBUILD' corresponds to a build configuration that I can select via the Configuration Manager (as opposed to the standard 'Release' build option).
The issue I'm having is that some of the code I've had to wrap this #if directive around lies in the WinForm.Designer.cs file in the region titled 'Windows Form Designer generated code'.
What seems to be occurring is that when I make a change to some of the form properties, this entire region of code seems to be deleted and re-generated, thereby removing the #if sections I had added.
- Am I going about this the right way?
- Is there a way to avoid the situation where I am losing the changes I've made in the WinForm.Designer.cs code?
I would really appreciate any advice from anyone with experience with conditional compilation and this sort of stuff.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
纳夫说道。通过将代码放入表单构造函数中来解决您的问题:
请注意,使用面板是使其上的所有控件不可见的简单方法。
Nuff said. Solve your problem by putting the code in the form constructor:
Note that using a panel is an easy way to make all the controls that are on it invisible.
#if 方法的一个问题是,您可能很快就会陷入这样的情况:对于不同的编译有多个 #if 语句。很快就很难判断哪个代码块与哪个项目相匹配。
更好的解决方案是识别所有通用代码并将它们保存在单独的项目文件夹中,例如 common/ 。然后,您可以在 server/ 下获得特定的服务代码,并在 client/ 下获得所有表单代码。
所有通用代码仍将存在于一个位置,并且您的服务器和客户端将变得更具可读性。您不再需要担心仅客户端的更改会影响服务器以及相反的情况。另外,通过正确组织文件夹,您可以将项目的内容(抽象概念,例如客户端/服务器)与项目的方式(具体实现,例如表单)分开。
An issue with the #if approach is that you can rapidly start falling into a situtation where you have multiple #if statements for different compiles. It very quickly becomes difficult to tell which chunk of code goes with which project.
A better solution would be to identify all common code and keep them in a separate project folder, such as common/ . Then you have the specific service code under server/ for example and all forms code under client/ .
All common code will still exist in one location and you server and clients become much more readable. You no longer have to worry about client only changes affecting the server and the opposite. Plus with proper organization of folders you can keep the what of your project (abstract concepts such as client/server) apart from the how (concrete implementations such as forms).
你无能为力。表单设计者将生成该代码,并以暴力方式完成,根本不关心您的需求或所做的更改。因此,您不应该破坏自动生成的代码。
您将需要采取一种新方法并弄清楚如何在不更改 autogen 代码的情况下完成您需要的工作,因为这绝对是一条纯粹的挫败之路。
There isn't much you can do. the Form designer is going to generate that code, and do it in a brute force way, with no care at all about your needs or changes you have made. You shouldn't muck with autogenerated code because of this.
You will need to take a new approach and figure out how to get what you need accomplished without altering the autogen code, as that's definitely a path of pure frustration.