c# - 在 Outlook VSTO 加载项中,C# 不允许您将事件处理程序 Hook 到 Application.Quit

标签 c# event-handling vsto

微软文档说 (https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2010/ee720183(v=office.14)#practice-2-detecting-when-outlook-is-shutting-down)

To detect that Outlook is shutting down, you can use the Quit event of the Application object in the Outlook object model to receive a notification that the process is shutting down. Be sure that you respond quickly to the event and return control to Outlook as quickly as possible.

但是好像只能在VB.Net中完成,C#不允许我。有一个名为 Quit() 的方法以及一个名为 Quit 的事件,当我尝试将事件处理程序连接到该事件时,C# 编译器表示您不能在方法组上使用 +=。

顺便说一句,我尝试过:您也无法在 C# 中创建具有相同名称的事件和方法的任何内容。语言不允许。

下面的示例是 Outlook 插件的 VSTO 项目模板代码,仅添加了 QuitHandler 方法,并尝试将其 Hook 到 Quit 事件。

当您输入代码时,智能感知将显示 Quit 事件和 Quit 方法以供选择,但选择事件会导致代码无法编译。

有没有办法明确告诉编译器您打算使用退出事件

namespace OutlookAddIn2
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.Quit += MyQuitHandler;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Note: Outlook no longer raises this event. If you have code that 
            //    must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
        }

        void MyQuitHandler()
        {

        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

编译器输出(您可以忽略 en-US 警告,就我的内存而言,我在所有版本的 Visual Studio 中都遇到过该警告,AFAICT 这是由于在同一台计算机上安装 Office 和 Visual Studio 引起的。)

1>------ Build started: Project: OutlookAddIn2, Configuration: Debug Any CPU ------
1>CSC : warning CS2038: The language name 'en-US' is invalid.
1>D:\Temp\OutlookAddIn2\ThisAddIn.cs(7,10,7,26): error CS1656: Cannot assign to 'Quit' because it is a 'method group'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

当我在 VB.Net 中尝试相同时,问题不存在,编译正常:

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        AddHandler Application.Quit, AddressOf MyQuitHandler
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Sub MyQuitHandler()

    End Sub

End Class

这是使用 Vusual Studio 2017 和 Resharper 2017.3.1。

我不认为 Resharper 在这种特殊情况下应该受到指责:当其符号缓存出现问题时,我看到过类似的错误消息,但这永远不会导致代码无法编译。相反,尽管代码编辑器指出了错误,它仍然可以编译并运行代码。

[编辑] 顺便说一句,它也不是这样工作的,我认为它是隐式的

Application.Quit += new ApplicationEvents_11_QuitEventHandler(MyQuitHandler);

最佳答案

No Application Quit event in Outlook? 的重复项

答案(我测试并确认有效):

((Outlook.ApplicationEvents_11_Event)Application).Quit 
+= new Outlook.ApplicationEvents_11_QuitEventHandler(ThisAddIn_Quit);

void ThisAddIn_Quit()
{
   System.Windows.Forms.MessageBox.Show("bye bye problem, I found the solution!!");
}

关于c# - 在 Outlook VSTO 加载项中,C# 不允许您将事件处理程序 Hook 到 Application.Quit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56537728/

相关文章:

javascript - 什么是 DOM 事件委托(delegate)?

c# - 事件处理程序会阻止垃圾收集的发生吗?

c# - FileOpenPicker 错误

c# - 获取 BigInteger 前 5 位数字的最快方法

c# - 如何使用 System.Text.Json 将 Newtonsoft JToken 序列化为 JSON?

C# KeyPress 事件方法未在 Visual Studio 中注册?还是代码?

perl - 为什么inotify会丢失事件?

c# - 在加载项表单中获取奇怪的字符

c# - 处理 Excel 互操作中的大选择范围

c# - 内存不足故障自动化 VSTO Powerpoint API