C# - 是否可以使用类而不是表单来启动我的项目?
我希望我的项目通过类而不是表单开始,有什么办法可以做到这一点吗?或者更准确地说,是否有任何好方法可以确保启动的第一个类(除了 Program 之外)不是表单类。
我尝试更改 Program.main() 中的类,但看起来 Application.run() 需要 ApplicationContext。
我想我可以更改程序类来启动另一个类,并让该类使用 Application.run() 启动表单,但我认为这会导致很多问题,因为我不希望启动相同的表单每次都首先,并且 Application.run() 必须至少使用一次,最多使用一次。所以我认为很难跟踪是否使用了 Application.run() 。
另一个可能更重要的问题是:这是在 .net 中执行操作的好方法吗?我想要这样做的原因是因为我想创建某种 MVC 项目,其中我想要开始的类是控制器,我将使用的所有表单都是视图。
I want my project to be started through an class instead of a form, is there any way to do this? Or to be more precise is there any good way to make sure that the first class, except Program, that is started isn't a form-class.
I tried to change to my class in Program.main() but it looks like Application.run() needs a ApplicationContext.
I guess that I could change the Program-class to start another class and let that class start the form with Application.run() but I think that it will cause a lot of problem since I don't want the same form to be started first each time and Application.run() have to be used at least once and at most once. So I think it will be hard to keep track of if Application.run() has been used or not.
Another question that might be even more important; Is this a good way to do things in .net? The reason I want to do so is because I want to create some sort of MVC project where the class I want to start with is the controller and all forms I'll use will be views.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制器的示例实现:
您可以像这样使用它:
同时检查 WindowsFormsApplicationBase 类作为控制器的良好基类。对单例应用程序和启动屏幕的良好支持。
A sample implementation of a controller:
You could use it like this:
Also check out the WindowsFormsApplicationBase class as a good base class for your controller. Nice support for singleton apps and splash screens.
要决定哪个类应首先运行,您只需将应用程序的 Main 方法放入该类中即可。
因此,基本上,创建一个新类,放入 Main 方法(并将其从 Program.cs 中删除)执行所需的逻辑,然后按如下方式启动窗口:
Form1 是必须启动的表单的名称。
To decide which class should run first, you should simply put in the Main method of your application in that class.
So basically, create a new class, put in the Main method (and remove it from Program.cs) do the logic you need and then launch the window as follows:
Form1 is the name of the form that has to be launched.