简体   繁体   English

C#.NET 4:如何从类文件中打开表单?

[英]C#.NET 4: How to open a form from inside a class file?

NOTE: I searched and searched for this but I could not find the exact thing I was looking for... 注意:我搜索并搜索了此内容,但找不到确切的东西...

So, I'm making a game that's kinda like Fallout but uses a form GUI instead of an actual first-person game environment. 因此,我正在制作一款类似于《辐射》(Fallout)但使用表单GUI而不是实际的第一人称游戏环境的游戏。 I need the program to load a class file before anything else, and I want that class file to open the main menu. 我需要该程序先加载一个类文件,并且希望该类文件打开主菜单。 How could I get Main.cs (the class file to load first) to open MainMenu.cs (the form to open)? 如何获取Main.cs (要首先加载的类文件)来打开MainMenu.cs (要打开的表单)?

This is what I attempted to do last: 这是我最后尝试做的:

Form MainMenu = new MainMenu();
MainMenu.Show();

Doing this brought up these errors: 这样做会带来以下错误:

The type or namespace name 'Form' could not be found (Are you missing a using directive or an assembly reference?) 'System.Windows.Forms.Form.Show(System.Windows.Forms.IWin32Window)' is a 'method' but is used like a 'type' 找不到类型或名称空间名称“ Form”(是否缺少using指令或程序集引用?)“ System.Windows.Forms.Form.Show(System.Windows.Forms.IWin32Window)”是一种“方法”但用作“类型”

The first error refers to this line: 第一个错误涉及以下行:

Form MainMenu = new MainMenu();

That code doesn't know what Form is. 该代码不知道什么是Form Unless it's in the same namespace (which it really shouldn't be in this case) then you need to either fully qualify it: 除非它在同一个命名空间中(在这种情况下实际上不应该这样),否则您需要完全限定它:

System.Windows.Forms.Form mainMenu = new MainMenu();

or add a using directive to the file: 或在文件中添加using指令:

using System.Windows.Forms;

As an additional step, if you haven't already done so, your class library will need an assembly reference to the System.Windows.Forms assembly. 另外,如果还没有这样做,则您的类库将需要对System.Windows.Forms程序集的程序集引用。 (Note that this tightly couples your class library to Windows Forms as a technology. So you won't be able to re-use the code without carrying a reference to Windows Forms.) (请注意,这作为一种技术将类库与Windows Forms紧密耦合。因此,如果不携带Windows Forms的引用,您将无法重用代码。)

If the project itself is fully aware of the types and you're simply missing the using directive, you might more simply use the specific type, even just implicitly: 如果项目本身完全了解类型,而您只是缺少using指令,则您可能更简单地使用特定类型,甚至隐式地使用:

var mainMenu = new MainMenu();

(Note also how I changed the variable name slightly in some of this code, which leads me to...) (另请注意,在某些代码中如何稍微更改了变量名,这导致我...)


The second error refers to this line: 第二个错误涉及此行:

MainMenu.Show();

MainMenu is a type . MainMenu是一种类型 And Show is an instance method on an object of said type. Show是上述类型对象的实例方法 This line makes it look like you're calling it as a static method or trying to use Show as a type itself. 该行使您看起来像是将其作为静态方法调用,还是试图将Show用作类型本身。 This is confusing the compiler. 这使编译器感到困惑。

In short, don't give your variables the same exact names as your types. 简而言之,不要为变量提供与类型相同的名称。 Instead: 代替:

Form mainMenu = new MainMenu();
mainMenu.Show();

Your code has some problems, but the breaker is naming an object the same as its class (MainMenu = new MainMenu()). 您的代码有一些问题,但是中断程序正在将对象命名为其类(MainMenu = new MainMenu())。 This is confusing the compiler and causing the other errors. 这会混淆编译器并导致其他错误。 The code should read 该代码应阅读

MainMenu myMainMenu = new MainMenu();
myMainMenu.Show();

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

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