C# - 是否可以使用类而不是表单来启动我的项目?

发布于 2024-09-01 07:17:56 字数 418 浏览 4 评论 0原文

我希望我的项目通过类而不是表单开始,有什么办法可以做到这一点吗?或者更准确地说,是否有任何好方法可以确保启动的第一个类(除了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一花一树开 2024-09-08 07:17:56

控制器的示例实现:

public class Controller : ApplicationContext {
    public Controller() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        mInstance = this;
    }
    public Controller Instance { get { return mInstance; } }
    public void Start() {
        Application.Run(this);
    }
    public void Exit() {
        this.ExitThread();
    }
    public void CreateView(Form frm) {
        Views.Add(frm);
        frm.FormClosed += FormClosed;
        frm.Show();
    }
    private void FormClosed(object sender, FormClosedEventArgs e) {
        Views.Remove(sender as Form);
        // NOTE: terminate program when last view closed
        if (Views.Count == 0) Exit();
    }
    private List<Form> Views = new List<Form>();
    private Controller mInstance;

}

您可以像这样使用它:

static class Program {
    [STAThread]
    static void Main() {
        var c = new Controller();
        c.CreateView(new Form1());
        c.Start();
    }
}

同时检查 WindowsFormsApplicationBase 类作为控制器的良好基类。对单例应用程序和启动屏幕的良好支持。

A sample implementation of a controller:

public class Controller : ApplicationContext {
    public Controller() {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        mInstance = this;
    }
    public Controller Instance { get { return mInstance; } }
    public void Start() {
        Application.Run(this);
    }
    public void Exit() {
        this.ExitThread();
    }
    public void CreateView(Form frm) {
        Views.Add(frm);
        frm.FormClosed += FormClosed;
        frm.Show();
    }
    private void FormClosed(object sender, FormClosedEventArgs e) {
        Views.Remove(sender as Form);
        // NOTE: terminate program when last view closed
        if (Views.Count == 0) Exit();
    }
    private List<Form> Views = new List<Form>();
    private Controller mInstance;

}

You could use it like this:

static class Program {
    [STAThread]
    static void Main() {
        var c = new Controller();
        c.CreateView(new Form1());
        c.Start();
    }
}

Also check out the WindowsFormsApplicationBase class as a good base class for your controller. Nice support for singleton apps and splash screens.

舟遥客 2024-09-08 07:17:56

要决定哪个类应首先运行,您只需将应用程序的 Main 方法放入该类中即可。

因此,基本上,创建一个新类,放入 Main 方法(并将其从 Program.cs 中删除)执行所需的逻辑,然后按如下方式启动窗口:

    [STAThread]
    static void FormLauncher()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

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:

    [STAThread]
    static void FormLauncher()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

Form1 is the name of the form that has to be launched.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文