文件夹上的C#(outlook加载项)上下文菜单

在我的VSTO outlook插件中,我正在尝试放置一个按钮,当我右键单击文件夹时会显示该按钮。 在我的启动function中我有这个:

Outlook.Application myApp = new Outlook.ApplicationClass(); myApp.FolderContextMenuDisplay += new ApplicationEvents_11_FolderContextMenuDisplayEventHandler(myApp_FolderContextMenuDisplay); 

然后我有那个处理程序…

 void myApp_FolderContextMenuDisplay(CommandBar commandBar, MAPIFolder Folder) { var contextButton = commandBar.Controls.Add(MsoControlType.msoControlButton, missing, missing, missing, true) as CommandBarButton; contextButton.Visible = true; contextButton.Caption = "some caption..."; contextButton.Click += new _CommandBarButtonEvents_ClickEventHandler(contextButton_Click); } 

最后点击的处理程序….

 void contextButton_Click(CommandBarButton Ctrl, ref bool CancelDefault) { //stuff here } 

我的问题是如何将myApp_FolderContextMenuDisplay中的contextButton_Click MAPIFolder Folder发送到contextButton_Click

(如果可以通过另一种方式做到这一点,我也愿意接受建议)

最简单的方法就是使用闭包,例如:

 // where Folder is a local variable in scope, such as code in post contextButton.Click += (CommandBarButton ctrl, ref bool cancel) => { DoReallStuff(ctrl, Folder, ref cancel); }; 

如果需要,请务必清理事件。 需要注意的一件事是,文件夹的RCW现在可能具有“延长的生命周期”,因为闭合可以比以前更长时间保持活动(但是在不需要时手动释放RCW 非常重要 。)

快乐的编码。