C#Windows Forms Designer代码生成 [英] C# Windows Forms Designer code generation

查看:376
本文介绍了C#Windows Forms Designer代码生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我要面对一个巨大的问题.我有一个C#项目(称为FP.Forms),这是我的应用程序的窗体"库,另一个包含控件的项目(称为FP.Controls).首先将默认名称空间设置为FP.Forms,将另一个命名空间设置为FP.Controls
在FP.Forms中添加了FP.Controls库作为参考.

最大的问题是Windows窗体设计器执行的代码生成...生成的代码无法编译.问题所在:

Hi guys!
I have a huge problem to face. I have a C# project (called FP.Forms) which is a ''forms'' library for my application and the other project containing controls (called FP.Controls). First of them has the default namespace set to FP.Forms, the other one to FP.Controls
FP.Controls library is added as a reference in FP.Forms.

The big problem is code generation done by windows forms designer... The generated code does not compile. Here''s where the problem exists:

this.MainPanel = new System.Windows.Forms.Panel();
this.gearboxMainPanel1 = new FP.Controls.NicePanel();




C#编译器将引发以下错误:错误CS0234:类型或名称空间名称"Controls"在名称空间"FP.Forms.FP"中不存在(您是否缺少程序集引用?)

我发现的是编译器在编译时以某种方式在代码中添加了项目的默认命名空间(即FP.Forms)...因此,编译器尝试编译"以下行:




The C# compiler throws the following error: error CS0234: The type or namespace name ''Controls'' does not exist in the namespace ''FP.Forms.FP'' (are you missing an assembly reference?)

What I''ve found is the compiler somehow adds at compile time the default namespace of the project (which is FP.Forms) to the code... so the compiler "tries to compile" the following line:

this.gearboxMainPanel1 = new FP.Forms.FP.Controls.NicePanel()



如果是的话...为什么在



If so... why the hell it does not throw error on the

<pre lang="cs">new System.Windows.Forms.Panel();</pre><br />


上它不会引发错误 ??

可以通过在每个命名空间之前添加global ::来解决该问题,但是由于生成了完整的代码,因此在使用Windows窗体设计器对窗体进行任何更改后,都会删除该遍历. 你们知道发生了什么吗?
提前感谢!


??

The problem can be walked around by adding global:: before each namespace, but since the whole code is generated, the walkaround will be erased after any change is done to the form using a windows forms designer.
Have you guys have any idea what is going on?
Thanx in advance!

推荐答案

首先,是项目的编译.该项目不包含源文件.另一个重要的部分是项目文件,它提供了引用程序集的集合.

进一步的问题取决于您如何编译代码.

我建议您从CodeDOM执行编译.引用的程序集作为字符串数组作为参数传递给CompilerParameters的构造函数.
请参见以下有关编译的代码示例:
http://msdn.microsoft.com/en-us/library/system. codedom.compiler.codedomprovider.aspx [ ^ ].

因此,这应该清除您的编译器在编译时以某种方式添加".没有莫名其妙",这是完全清楚的.

现在,在一种情况下编译时错误在另一种情况下不显示错误又如何呢?很简单.

这需要引用程序集System.Windows.Forms:

First, the compilation of the project. The project does not consist of the source files along. Another important part is project file which provides the set of references assemblies.

Further issues depend on how you compile the code.

I would recommend you performing compilation from CodeDOM. Referenced assemblies are passed as an array of strings as a parameters to the constructor of CompilerParameters.
See this code sample on the compilation:
http://msdn.microsoft.com/en-us/library/system.codedom.compiler.codedomprovider.aspx[^].

So, this should clear your "compiler somehow adds at compile time". There is not "somehow", it''s totally clear.

Now, how about compile-time error in one case not not showing error in another case? Simple.

This requires referencing of the assembly System.Windows.Forms:

var something = new System.Windows.Forms.Panel();



显然,如果没有错误,则此程序集为引用.
这是完全不同的:



Apparently, if there is no error, this assembly is references.
This is completely different:

this.gearboxMainPanel1 = new FP.Forms.FP.Controls.NicePanel();



如果需要其他组装,则由您开发.您需要引用它,这不是很明显吗?

问题解决了.

—SA



If requires some other assembly, the one you develop. You need to reference it, isn''t that obvious?

Problem solved.

—SA


这篇关于C#Windows Forms Designer代码生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆