gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:54:08 25 4
gpt4 key购买 nike

微软文档说( 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,并尝试将其挂接到 Quit 事件。

当您输入代码时,intellisense 将同时显示 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 中都遇到过这种情况,据我所知,这是由于在同一台机器上安装 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# 不允许您将事件处理程序挂接到 Application.Quit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56537728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com