本文介绍了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!

推荐答案

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代码生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:50