HOW TO:回應在特定專案中的事件 (Visual C#)

Automation 模型包含可以用來回應 Visual Studio 整合式開發環境 (IDE) 中各種環境事件的物件。 在 VSLangProjVSLangProj80 中定義的環境事件,是 Visual C# 和 Visual Basic 專案特有的事件。 例如,在 Visual Basic 專案中加入或移除 Import 物件時,會引發 ImportsEvents 事件。

這個範例會使用 Visual C#,將某個專案類型特有的 ReferencesEvents 事件處理常式加入至增益集專案。 當 Visual C# 或  Visual Basic 專案中變更、加入或移除參考時,會引發 ReferencesEvents 事件。

注意事項注意事項

根據您目前使用的設定或版本,您所看到的對話方塊與功能表指令可能會與 [說明] 中描述的不同。 使用 [一般開發設定] 開發了這些程序。 若要變更設定,請從 [工具] 功能表中選取 [匯入和匯出設定]。 如需詳細資訊,請參閱 使用設定

若要使用 Visual C# 處理與參考相關的事件

  1. 在 Visual C# 中建立 Visual Studio 增益集專案。

  2. 將 using VSLangProj; 加入至 Connect.cs 檔案的最上方。

  3. 在 [專案] 功能表上按一下 [加入參考],再按一下 [.NET] 索引標籤,選取第一個 [VSLangProj],然後再按 [確定]。

  4. 在 Connect 類別 (Class) 中,初始化一個處理 ReferencesEvents 物件的變數以及另一個處理 OutputWindowPane 物件的變數。

        private DTE2 _applicationObject;
        private AddIn _addInInstance;
        private VSLangProj.ReferencesEvents refEvents;
        private OutputWindowPane outputWinPane;
    

    在這個範例中,變數的名稱為 refEvents。

    Automation 模型中的其他物件則是與特定專案中其他類型的事件相關。 例如,在 Imports 集合中加入或移除 Import 物件時,會引發 ImportsEvents 事件。 BuildManagerEvents 適用於從自訂工具輸出所建置之暫存組件的相關事件。 如需 BuildManager 物件的詳細資訊,請參閱 BuildManager 物件簡介; 如需因專案類型而異之所有事件的完整清單,請參閱事件物件 (因各種專案類型而異);如需一般 Automation 事件的清單,則請參閱 Automation 事件物件

  5. OnConnection 方法中,初始化變數以攔截事件。 在這個範例中,變數的名稱為 events。

    EnvDTE80.Events2 events = 
    (EnvDTE80.Events2)_applicationObject.Events;
    
  6. OnConnection 方法中,初始化 OutputWindow 變數。

    OutputWindow outputWindow = (OutputWindow)_applicationObject.Windows.Item
    (Constants.vsWindowKindOutput).Object;
    outputWinPane = outputWindow.OutputWindowPanes.Add
    ("ReferencesEvents Event Information");
    
  7. 同樣在 OnConnection 方法中,從 Automation 模型中擷取事件物件 (Event Object)。

    refEvents = 
    (VSLangProj.ReferencesEvents)events.GetObject
    ("CSharpReferencesEvents");
    

    在這個範例中,ReferencesEvents 是 Visual C# 專案特有的事件。 若要回應 Visual Basic 特有的事件,則請以 VBReferencesEvents 取代 CSharpReferencesEvents 字串。 如需如何判斷因不同專案類型而異之事件應使用何種字串的詳細資訊,請參閱事件物件 (因各種專案類型而異)

  8. 使用 += 運算子連接至由步驟 3 中所擷取之事件物件公開 (Expose) 的各個委派 (Delegate)。 例如,若要連接由 ReferenceAdded 事件公開的委派,請使用下列程式碼:

    refEvents.ReferenceAdded += new 
    _dispReferencesEvents_ReferenceAddedEventHandler
    (this.ReferenceAdded);
    
  9. 為事件物件的各個相關事件加入程序。 例如,若要處理在加入參考時發生的事件,請使用下列程式碼:

    public void ReferenceAdded( VSLangProj.Reference addedRef ) 
    { 
        outputWinPane.OutputString( "ReferencesEvents.ReferenceAdded" 
    + "\n" ); 
        outputWinPane.OutputString( "The reference to " + addedRef.Name
     + " was added." + "\n" ); 
    }
    

    ReferencesEvents 為例,您必須為下列項目定義事件:

    在下列完整的程式碼範例中會包含這些事件。

  10. 最後,為了不讓 Visual Studio 在您關閉增益集之後繼續監視與視窗相關的事件而減慢系統速度,請停用事件處理功能。 在 Visual C# 中,停用事件處理功能的方式是使用 -= 運算子。 例如,若要停用 ReferenceAdded 的事件處理功能,請使用下列程式碼:

    refEvents.ReferenceAdded -= new
     _dispReferencesEvents_ReferenceAddedEventHandler
    (this.ReferenceAdded);
    

    這麼做的話,不論是關閉增益集,或是當增益集仍在執行時關閉 IDE,都會關閉事件處理功能。 當 IDE 關閉時,所有執行中的增益集會先自動關閉。

範例

下列是基本 Visual Studio 增益集的範例,在此範例中會示範如何在 Visual Studio 中攔截及處理 Visual C# 參考事件。 每當發生參考事件時,就會傳送通知訊息至 [輸出] 視窗。

using System;
using Microsoft.VisualStudio.CommandBars;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using VSLangProj;

public Connect()
  {
  }
  public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
  {
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    // Retrieve the event objects from the automation model.
    EnvDTE80.Events2 events = 
(EnvDTE80.Events2)_applicationObject.Events;
    // Send event messages to the Output window.
    OutputWindow outputWindow =
 (OutputWindow)_applicationObject.Windows.Item
(Constants.vsWindowKindOutput).Object;
    outputWinPane = 
outputWindow.OutputWindowPanes.Add
("ReferencesEvents Event Information");

    // Retrieve the event objects from the automation model.
    refEvents = (VSLangProj.ReferencesEvents)
events.GetObject("CSharpReferencesEvents");

    // Connect to each delegate exposed from each object 
    // retrieved above.
    refEvents.ReferenceAdded += new
 _dispReferencesEvents_ReferenceAddedEventHandler
(this.ReferenceAdded);
    refEvents.ReferenceChanged += new
 _dispReferencesEvents_ReferenceChangedEventHandler
(this.ReferenceChanged);
    refEvents.ReferenceRemoved += new
 _dispReferencesEvents_ReferenceRemovedEventHandler
(this.ReferenceRemoved); 
  }

  public void OnDisconnection(Extensibility.ext_DisconnectMode
 disconnectMode, ref System.Array custom)
  {
    // If the delegate handlers have been connected, then 
    // disconnect them here. 
    // If you do not do this, the handlers may still 
    // fire because they have not been garbage collected.
    if (refEvents != null)
    {
        refEvents.ReferenceAdded -= new
 _dispReferencesEvents_ReferenceAddedEventHandler
(this.ReferenceAdded);
        refEvents.ReferenceChanged -= new
 _dispReferencesEvents_ReferenceChangedEventHandler
(this.ReferenceChanged);
        refEvents.ReferenceRemoved -= new
 _dispReferencesEvents_ReferenceRemovedEventHandler
(this.ReferenceRemoved); 
    }
  }

  // References related events.
  public void ReferenceRemoved( VSLangProj.Reference removedRef ) 
  { 
    outputWinPane.OutputString( "ReferencesEvents.ReferenceRemoved"
 + "\n" ); 
    outputWinPane.OutputString( "The reference to " + removedRef.Name
 + " was removed." + "\n" ); 
  } 

  public void ReferenceChanged( VSLangProj.Reference changedRef ) 
  { 
    outputWinPane.OutputString( "ReferencesEvents.ReferenceChanged" 
+ "\n" ); 
    outputWinPane.OutputString( "The reference to " + changedRef.Name 
+ " was changed." + "\n" ); 
  } 
        
  public void ReferenceAdded( VSLangProj.Reference addedRef ) 
  { 
    outputWinPane.OutputString( "ReferencesEvents.ReferenceAdded" +
 "\n" ); 
    outputWinPane.OutputString( "The reference to " + addedRef.Name 
+ " was added." + "\n" ); 
  } 
  public void OnAddInsUpdate(ref System.Array custom)
  {
  }
  public void OnStartupComplete(ref System.Array custom)
  {
  }
  public void OnBeginShutdown(ref System.Array custom)
  {
  }
  private DTE2 _applicationObject;
  private AddIn _addInInstance;
  private VSLangProj.ReferencesEvents refEvents;
  private OutputWindowPane outputWinPane;
  }}

編譯程式碼

若要編譯此程式碼,請在 Visual C# 中建立新的 Visual Studio 增益集專案,並以範例中的程式碼取代 Connect 類別的程式碼。 如需如何執行增益集的詳細資訊,請參閱 HOW TO:使用增益集管理員來控制增益集

請參閱

工作

HOW TO:回應在特定專案中的事件 (Visual Basic)

參考

+= 運算子 (C# 參考)

-= 運算子 (C# 參考)

其他資源

回應 Automation 事件

回應事件 (Visual Basic 和 Visual C# 專案)