如何退出 VSTO 加载项

And*_*huk 5 .net vsto outlook-addin

有没有比抛出异常更好的方法从加载项代码中关闭 Outlook 2010 的 VSTO 加载项?
我不喜欢抛出异常,因为 Outlook 可能认为我的加载项不稳定。


---编辑:---
关闭是指停止执行加载项代码并隐藏其 UI 或停用。但我希望在重新启动 Outlook 后启用它

woo*_*ddy 3

当您在 VS2010 中创建 VSTO 项目时,应该会在您的ThisAddIn.cs. 如果没有,您可能想自己添加它们。

/// <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);
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
     //execute your code here, e.g. output some values to a text file 
}
Run Code Online (Sandbox Code Playgroud)

您可以将代码放置在 ThisAddIn_Shutdown 事件中,并仅在加载项关闭时执行它。

编辑:MSDN 是这么说的:

从 Outlook 2010 开始,默认情况下,Outlook 不会向加载项发出正在关闭的信号。具体来说,Outlook 在快速关闭期间不再调用 IDTExtensibility2 接口的 OnBeginShutdown 和 OnDisconnection 方法。同样,当 Outlook 关闭时,使用 Microsoft Visual Studio Tools for Office 编写的 Outlook 加载项不再调用 ThisAddin_Shutdown 方法。

更多详细信息请参见:
http://msdn.microsoft.com/en-us/library/office/ee720183.aspx#OL2010AdditionalShutdownChanges_AddinShutdownChangesinOL2010Beta