VSTO 加载项窗体关闭处理程序

VSTO Addin Form Closing Handler

我有一个适用于 outlook 2013 的 VSTO 插件。我正在尝试使用事件处理程序为表单关闭事件注册一个事件。

这是我在 class Form1 中的代码:

    public delegate void MyEventHandler();
    private event MyEventHandler Closing;

    private void OtherInitialize()
    {
        this.Closing += new MyEventHandler(this.Form1_Closing);
    }

同样来自class Form1:

    public Form1()
    {
        InitializeComponent();
        OtherInitialize();
    }

    private void Form1_Closing(object sender, CancelEventArgs e)
    {
        // Not sure what to put here to make the application exit completely
        // Looking for something similar to Pytthon's sys.exit() or
        // Applicaton.Exit() in Forms Applicatons, I tried
        // Applicaton.Exit() it did not work
    }

当我 运行 出现此错误和警告时:

警告:

Form1.Closing hides inherited member System.Windows.Forms.Form.Closing.  Use the new keyword if hiding was intended

错误:

No overload for Form1_Closing matches delegate System.EventHandler

这些errors/warnings是什么意思?当使用 X 按钮或 form.Close() 关闭表单时,如何正确注册 Form1_Closing 事件处理程序现在我可以调用 form.Close() 但它不会' 似乎触发了 Form1_Closing 事件。

无需声明 Closing 事件,因为父级 class 提供了开箱即用的事件。此外,您可以简单地设置事件处理程序而无需声明委托 class(最新的 .net 版本):

public Form1()
{
    InitializeComponent();
    OtherInitialize();
}

private void OtherInitialize()
{
    Closing += Form1_Closing;
}

private void Form1_Closing(object sender, CancelEventArgs e)
{
    // Not sure what to put here to make the application exit completely
    // Looking for something similar to Pytthon's sys.exit() or
    // Applicaton.Exit() in Forms Applicatons, I tried
    // Applicaton.Exit() it did not work
}