2012-11-23 26 views
1

下面是一些示例代碼:在這個Outlook 2007應用程序中,Explorers.NewExplorer事件怎麼沒有被觸發?

private Outlook.Application applicationObject; 
    public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) 
    { 
     MessageBox.Show("on connection"); 
     applicationObject = (Outlook.Application)application; 
     applicationObject.Explorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer); 
    } 

    void Explorers_NewExplorer(Microsoft.Office.Interop.Outlook.Explorer Explorer) 
    { 
     MessageBox.Show("new explorer"); 
    } 

「新資源管理器」的消息從來沒有出現在屏幕上,因爲NewExplorer事件永遠不會觸發,即使是在我點擊「在新窗口打開」。

什麼可能是錯的?

回答

1

您訂閱NewExplorer事件的Explorers實例可能正在進行垃圾回收。爲防止這種情況發生,請通過實例變量保留對其的引用:

private Outlook.Application applicationObject; 
private Outlook.Explorers explorers; 

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) 
{ 
    MessageBox.Show("on connection"); 
    applicationObject = (Outlook.Application)application; 
    explorers = applicationObject.Explorers; 
    explorers.NewExplorer += new Microsoft.Office.Interop.Outlook.ExplorersEvents_NewExplorerEventHandler(Explorers_NewExplorer); 
} 
+0

我不會猜到它。感謝您的幫助:) –

+0

沒問題:-)把它作爲一個通用規則:無論您何時訂閱COM對象的事件,都需要保持該對象活着。 – Douglas

相關問題