【发布时间】: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方法。