2014-12-19 68 views
0

我花了最後時間瞭解如何訂閱任何Outlook資源管理器的BeforeMinimize和BeforeMaximize事件並失敗。我到目前爲止所做的:VSTO Outlook Explorer BeforeMinimize,BeforeMaximize事件不會觸發

public partial class ThisAddIn 
{ 
    Outlook.Explorer explorer; 
    Outlook.Application application; 
    Outlook.ExplorerEvents_10_BeforeMinimizeEventHandler beforeMinimizeEventHandler; 

    ThisAddin_Startup() 
    { 
     //... create custom Task pane 

     application = Globals.ThisAddIn.Application; 
     explorer = application.ActiveExplorer(); 
     beforeMinimizeEventHandler = new Outlook.ExplorerEvents_10_BeforeMinimizeEventHandler(explorer_BeforeMinimize); 
     explorer.BeforeMinimize += beforeMinimizeEventHandler; 

    } 

    void explorer_BeforeMinimize(ref bool Cancel) 
    { 
     System.Windows.Forms.MessageBox.Show("BeforeMinimize"); 
     Cancel = true; 
    } 
} 

事件永遠不會被解僱。我也試過其他方法,比如鑄造explorerOutlook.ExplorerEvents_10_Event然後訂閱。我也檢查了只有一個資源管理器。儘管如此,沒有什麼可行 我做錯了什麼?感謝您的幫助。

回答

0

對你的代碼做了一些修改,現在我發現所有的東西都可以工作。

我已經將事件處理程序添加到Explorer.BeforeMinimize直接,而不是您已添加。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml.Linq; 
using Outlook = Microsoft.Office.Interop.Outlook; 
using Office = Microsoft.Office.Core; 

namespace OutlookAddIn4 
{ 
    public partial class ThisAddIn 
    { 

     Outlook.Explorer explorer; 
     Outlook.Application application; 

     private void ThisAddIn_Startup(object sender, System.EventArgs e) 
     { 
      application = Globals.ThisAddIn.Application; 
      explorer = application.ActiveExplorer(); 
      explorer.BeforeMinimize += explorer_BeforeMinimize; 
     } 

     void explorer_BeforeMinimize(ref bool Cancel) 
     { 
      System.Windows.Forms.MessageBox.Show("BeforeMinimize"); 
      Cancel = true; 
     } 

     private void ThisAddIn_Shutdown(object sender, System.EventArgs e) 
     { 
     } 

     #region VSTO generated code 

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

     #endregion 
    } 
} 

編譯後,啓動代碼被編譯成

private void ThisAddIn_Startup(object sender, EventArgs e) 
{ 
    this.application = Globals.ThisAddIn.Application; 
    this.explorer = this.application.ActiveExplorer(); 
    (new ComAwareEventInfo(typeof(ExplorerEvents_10_Event), "BeforeMinimize")).AddEventHandler(this.explorer, new ExplorerEvents_10_BeforeMinimizeEventHandler(this.explorer_BeforeMinimize)); 
}