2011-04-11 91 views
6

在我的VSTO outlook插件中,我試圖將一個按鈕顯示出來,當我右鍵單擊文件夾。在我啓動功能,我有這樣的:文件夾中的C#(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

(如果可以做到這一點的另一種方式,我很開放的建議太)

回答

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是非常重要的在不需要時手動釋放RCW )

快樂編碼。

相關問題