【问题标题】:Form Load Not Firing for form that has been hidden?表单加载未触发已隐藏的表单?
【发布时间】:2012-08-01 15:19:41
【问题描述】:

我正在编写一个必须隐藏主窗体的应用程序,但它在启动时会显示一个对话框。

我的主窗体在构造函数调用的 initialize() 方法中有以下行。

this.Load += new System.EventHandler(this.MainForm_Load);

我已经验证上面的代码命中了但 MainForm_Load() 方法从未被调用过。

这可能是因为我隐藏了表单吗?

我在 Program.cs 的 Main 中执行以下行:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

并在表单中覆盖了以下方法:

protected override void SetVisibleCore(bool value)
{
    _logger.Debug("Hiding the main window");
    base.SetVisibleCore(allowShowDisplay ? value : allowShowDisplay);
}

其中allowShowDisplay设置为false;

我在this queston 的回答中至少找到了该解决方案的一部分,并在另一个项目中使用了它。该项目虽然没有使用表单加载事件。我正在做的那个。

更新 这就是 Main 方法的样子。我正在尝试将依赖项注入所有元素。我已更改名称以删除客户名称。

[STA线程] 静态无效主要() {

        ServiceHost incomingPipeHost = new ServiceHost(typeof(ScreenPopService));
        incomingPipeHost.Open();

        XmlConfigurator.Configure();
        _logger.Debug("Starting Application");
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        _logger.Debug("Creating subformView"); 
        ISubformView subformView = new SubformView();

        _logger.Debug("Creating MainForm mainForm");
        MainForm mainForm = new MainForm();

        _logger.Debug("Creating MonitorController");
        IMonitorController MonitorController = new MonitorController();

        _logger.Debug("Adding View to MonitorController");
        MonitorController.View = mainForm;

        _logger.Debug("Adding subFormView to mainForm");
        mainForm.SubFormView = subFormView;

        _logger.Debug("Adding MonitorController to mainForm");
        mainForm.MonitorController = MonitorController;

        _logger.Debug("Loading Properties");
        IProperties properties = PropertiesManager.LoadProperties();

        _logger.DebugFormat("Loaded Properties [{0}]", properties);
        _logger.Debug("Setting properties on mainForm");
        mainForm.Properties = properties;

        _logger.Debug("Setting properties on MonitorController");
        MonitorController.Properties = properties;

        _logger.Debug("Settting ScreenPop Consumer on MonitorCotroller");
        MonitorController.screenPopConsumer = ScreenPopCallBackManager.Instance;

        _logger.Debug("Debug Running Application");

        Application.Run(mainForm);
    }

【问题讨论】:

  • 您是否正在创建新表单的实例..?如果是这样,请显示适用于手头问题/问题的代码..
  • 我正在创建一个表单实例,向它添加一堆参数,然后使用表单作为参数调用 Application.Run。该表单还有一个与之关联的通知图标正在显示。
  • 我发布的示例我正在工作,但不确定它是否是您正在寻找的东西..注意我如何创建表单的实例..您可以做同样的事情并以这种方式覆盖/设置值还有..
  • 我没有意识到你在主窗体可见之前做了这么多逻辑。我需要再次查看你的代码,因为我在你粘贴/编辑所有之前发布了我的答案新代码..给我几分钟看看它
  • 与其处理Load事件,不如重写OnLoad方法。

标签: c# winforms


【解决方案1】:

这是设计使然。窗体的 Visible 属性(以及相关的 SetVisibleCore 和 Show 方法)在 Winforms 中是一个大问题。在典型的 .NET 懒惰方式中,这就是创建具体窗口的原因。或者换句话说,Winforms 不会创建窗口,除非它必须

通过覆盖 SetVisibleCore() 并强制 value 属性为 false,您可以阻止这种情况的发生。窗口确实没有被创建。还有一堆其他的事情没有发生,包括触发 Load 事件。并且调用表单的 Close() 方法不会像通常那样停止程序。

这都是正常的行为。如果您的 Load 事件处理程序包含重要的内容,请确保将其移至构造函数。你只需要 Load 就知道窗口的大小和位置。

【讨论】:

    【解决方案2】:

    我现在在我的 Program.cs 文件中发布这个作为我将如何使用 MDI 这就是我的代码的样子..

    静态类程序

    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                Application.Run(new MainForm());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n\n" + ex.GetType().ToString(), "Error in MainForm");
    
            }
        }
    }
    

    现在我想隐藏然后显示的表单中的代码将在我的 MainForm 中包含以下代码这将是我正在执行的一个示例,如果表单加载并且我使用例如菜单单击事件。但 MDI 是您想要查看的内容。更改示例以适用于您的用例。

        private void newStudentRecordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                StudentForm studForm = new StudentForm();
                studForm.MdiParent = this;
                studForm.Show();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n\n" + ex.GetType().ToString(), "Error in Student Form");
            }
        }
    

    【讨论】:

    • 当然,我如何初始化主窗体并不重要(在一行中,它是自身或在 Application.Run() 方法中。我仍在调用 Application.Run()
    • Omar .. 看看我的 MDI Example Calling Application.Run with a single form 我同意你的说法.. 但是使用多种形式你的假设有点不对..
    • 我没有使用 MDI 打开子表单,而是使用 ShowDialog 打开它们,因为我更关心 DialogResponse 而不是表单。我认为您可能正在尝试解决与我所看到的不同的问题。
    【解决方案3】:

    我可以建议重新检查 MainForm_Load 的方法签名。签名应类似于“MainForm_Load(object sender, System.EventArgs e)”

    这可能听起来很蹩脚,但我遇到过这样的问题会占用时间:)

    如果这有帮助,请告诉我。谢谢!

    【讨论】:

    • mainform_load方法是IDE添加的,我没有编辑签名。我只是仔细检查了它,它是正确的。
    【解决方案4】:

    我最终寻求的似乎有效的解决方案是从对 Application.Run() 的调用中删除 mainForm,并在调用 Application.Run() 之前立即调用 mainForm.show(),然后调用 mainForm.Hide()。运行()

    mainForm.Show();
    mainForm.Hide();
    
    Application.Run();
    

    似乎工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 2020-01-11
      • 1970-01-01
      相关资源
      最近更新 更多