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

6

在我的VSTO Outlook插件中,我正在尝试放置一个按钮,在右键单击文件夹时会显示出来。在我的启动函数中,我有以下代码:

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
}

我的问题是如何将MAPIFolder FoldermyApp_FolderContextMenuDisplay发送到contextButton_Click?(如果有其他方法可以实现,我也很乐意听取建议)
1个回答

3
最简单的方法是使用闭包,例如:
// 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可能会有“扩展寿命”,因为闭包可能会使其比以前更长时间地保持活动状态(但在OOM时,手动释放RCWs非常重要,以防不必要的资源占用)。

编码愉快。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接