代码之家  ›  专栏  ›  技术社区  ›  Adam Kane

如何使申请有表格而不是表格?

  •  5
  • Adam Kane  · 技术社区  · 14 年前

    我希望我的C#.NET应用程序有一个表单,但不是表单。

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    

    相反,我想启动我的程序,然后它可以显示一个窗体,但不是窗体本身。换句话说,我不希望applicatin的主控制器是表单,而是希望它是一个非可视的逻辑容器,它能够显示表单,但不是表单本身。

    6 回复  |  直到 14 年前
        1
  •  2
  •   Derek Greer    14 年前

    创建一个单独的引导程序组件非常常见,您可以将主窗体的显示移动到:

    using System;
    using System.Windows.Forms;
    
    namespace Example
    {
        internal static class Program
        {
            [STAThread]
            private static void Main()
            {
                new Bootstrapper().Run();
            }
        }
    
        public class Bootstrapper
        {
            public void Run()
            {
                // [Application initialization here]
                ShowView();
            }
    
            private static void ShowView()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    
        2
  •  7
  •   Marc Gravell    14 年前

    你可以用 Application.Run() 使消息循环运行。但你需要这样做

        3
  •  5
  •   Hans Passant    14 年前

    您可以改用ApplicationContext。一旦您决定创建一个窗体,它将为您提供必要的消息循环,使窗体保持活动状态。使您的程序类类似于:

    static class Program {
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            AppContext = new ApplicationContext();
            Application.Run(AppContext);
        }
        public static void Quit() {
            AppContext.ExitThread();
        }
        public static ApplicationContext AppContext;
    }
    

    请注意,当您关闭最后一个窗口时,应用程序不会自动关闭。需要显式调用ExitThread。

        4
  •  2
  •   Tim Coker    14 年前

    正如Mark\u gravel提到的,Application.Run()会一直阻塞,直到Form1关闭。您可以在单独的线程上打开窗体,但该线程基本上会被窗体占用。当你想要exe退出时,你必须手动终止每个线程。请参见以下代码(它不会创建控制台窗口。我是通过创建一个默认的WinForms应用程序并更改程序类来实现的)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
    
            static List<Thread> threads = new List<Thread>();
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                for (int i = 0; i < 10; i++)
                {
                    StartThread();
                    System.Threading.Thread.Sleep(500);
                }
                //kill each thread so the app will exit, otherwise, the app won't close
                //until all forms are manually closed...
                threads.ForEach(t => t.Abort());
            }
    
            static void StartThread()
            {
                Thread t = new Thread(ShowForm);
                threads.Add(t);
                t.Start();
            }
    
            static void ShowForm()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }
    }
    
        5
  •  1
  •   Jeff Tindall    14 年前

    创建一个应用程序作为控制台应用程序,然后调用应用程序。运行马克说,当你需要一个表格。

        6
  •  0
  •   RobertPitt    14 年前

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(AppController.Instance);
    }
    

    在AppController.cs中

    namespace MyApplication
    {
        public class AppController
        {
             static AppController _AppController;
    
             public LoginWIndow LoginWIndow;
    
             //Constructor
             public void AppController()
             {
                 //Do what you will here, Start login form, bind events, w.e :)
    
                 if(true) //Your check
                 {
                     ShowLoginWindow();
                 }
             }
    
             public void ShowLoginWindow()
             {
                 LoginWIndow = new LoginWIndow();
                 LoginWIndow.ClosedForm += new FormClosedEventHander(ExitApplication);
                 LoginWIndow.Show();
             }
    
             public void ExitApplication(Object Sender, FormClosedEventArgs Args) 
             {
                //Some shutdown login Logic, then
                Application.Exit();
             }
    
             static AppController Instance
             {
                get
                {
                     if(_AppController == null)
                     {
                         _AppController = new AppController();
                     }
                     return _AppController;
                }
             }
        }
    }