【问题标题】:How do I get a class to run first in a Win Forms App如何让一个类首先在 Winforms 应用程序中运行
【发布时间】:2014-05-17 22:39:32
【问题描述】:

我需要在应用程序启动时运行一个名为 StartUp.cs 的类。我希望该类将值分配给另一个名为 MyAppSettings.cs 的类。现在,当 Form frmMainConsole 加载时,我希望它从 Settings 中读取值,因为它会影响 Form frmMainConsole 上的控件。我尝试了几种方法,并意识到我需要帮助。

【问题讨论】:

  • 我认为您需要发布您的代码,以便我们了解您迄今为止所做的尝试。这样可能更容易为您指出解决方案。
  • 是的,然后发布您尝试过的代码。还。把问题说清楚,我看不懂
  • 这里是表单加载方法private void frmMainConsole_Load(object sender, EventArgs e) { CreateGroupBox(); //设置初始设置 MyAppSetting MyAppSetting = new ChessStrategyGame.MyAppSetting(); moveToolStripMenuItem.Checked = MyAppSetting.ShowMoves; }
  • 您有一个StartUp.cs 可以为MyAppSettings.cs 分配一些值吗? StartUp.cs 是从哪里获取这些值的?

标签: c# .net winforms startup main


【解决方案1】:

如果您查看Program.cs,您会看到如下内容:

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

您可以简单地在Application.EnableVisualStyles();上方添加您的 StartUp.cs 调用

【讨论】:

    【解决方案2】:

    正如 cmets 所说 - 发布一些您尝试过的代码,但是,看看以下是否是您需要的

    public class InitialiseSettings
    {
        public static void Initialise()
        {
            //Code to run here
        }
    
        public InitialiseSettings()
        {
    
        }
    }
    
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            //Instantiate an object here of the class or call a static function of that class etc etc
            InitialiseSettings settings = new InitialiseSettings();
            InitialiseSettings.Initialise();
    
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            //Or you could do it here - before the form is displayed
        }
    }
    

    或者您可以在表单被实例化之前调用您的类 (Program.cs)

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Invoke class here
            Application.Run(new Form1());
        }
    }
    

    【讨论】:

      【解决方案3】:

      虽然可以修改 Program.cs 文件,但您可能还想在您的 StartUp 类中添加一个静态 void Main() 方法,并将其作为您项目的起点。这将使您最终控制应用程序的启动方式。

      您可以在项目设计器的应用程序页面中设置启动对象。请注意,这也意味着您将无法使用默认的 winform 应用程序框架。但是,如果您寻求对应用设置的更多控制,那么这可能是最好的选择。

      请参阅this link 了解更多信息。

      【讨论】:

        【解决方案4】:

        代码的错误部分是

        //Set Initial Settings 
        MyAppSetting MyAppSetting = new ChessStrategyGame.MyAppSetting(); 
        

        这样你每次都会创建一个新对象。

        解决办法:

        如果您将设置类“MyAppSettings.cs”设为静态类,它会对您有所帮助。 通常将设置类创建为静态,以便您设置一次值并可以通过应用程序访问属性/值。

        例如

        [STAThread]
        static void Main()
        {
            // create object of startup class and call initialize method
            StartUp newStartUp = new StartUp();
            newStartup.Initialize();
        
            Application.Run(new Form1());
        }
        
        
        StartUp.cs
        ----------
        public class StartUp
        {
        
            public void Initialize()
            {
                MyAppSettings.ShowMoves = true;    
            }
        
        }
        
        MyAppSettings.cs
        ----------------
        public static MyAppSettings
        {
            public static bool ShowMoves {get; set;}
        }
        
        Form1.cs
        --------
        private void Form1_Load(object sender, EventArgs e)
        {
            CreateGroupBox();
        
            //Set Initial Settings 
            movesToolStripMenuItem.Checked = MyAppSetting.ShowMoves;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-08-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-06
          • 2023-03-05
          相关资源
          最近更新 更多