2013-03-10 36 views
2

我正在開發Visual Studio 2010插件,並且此時我陷入困境。我希望收到關於特定項目物品變更的通知。例如,我需要知道是否在我的項目中添加了新窗體(winform)。如何通過視覺工作室插件追蹤項目項目的屬性更改

有兩種可能性如何添加一個表單到項目在Visual Studio 2010:

  1. 你可以簡單的通過上下文菜單中添加一個新的表格項目。在這種情況下,IDE引發 ProjectItemAdded事件。問題是,當時新表格被識別爲 eFileTypeCppHeader而不是 eFileTypeCppForm

  2. 第二個選項是通過屬性選項卡將頭文件的FileType屬性更改爲「 C++表格」。沒有通知加載項關於此屬性更改的事件。

那麼,我的外接程序如何知道ProjectItem的屬性已更改?

回答

1

最後我發現瞭如何綁定到項目屬性已更改的事件。有必要實現 IVsHierarchy接口,然後執行 OnPropertyChanged事件。還有必要通過已打開的項目列舉並逐一綁定其財產變更。看下面的例子。

public ref class Hierarchy: public IVsHierarchyEvents 
{ 
private: 
    IVsHierarchy^ TargetHierarchy; 
    unsigned int TargetHierarchyCookie; 

public: 
    Hierarchy(IVsHierarchy^ THierarchy) 
    { 
     TargetHierarchy = THierarchy; 
     TargetHierarchy->AdviseHierarchyEvents(this, TargetHierarchyCookie); 
    } 

    virtual int OnPropertyChanged(unsigned int itemid, int propid, unsigned int flags) 
    { 
     // your code here 
    } 

    ... 
}; 




public ref class Connect : public IDTExtensibility2, public IDTCommandTarget 
{ 
private: 
    List<Hierarchy^>^ Hierarchies; 

    ... 

public: 
    virtual void OnConnection(...) 
    { 
     appObject = dynamic_cast<DTE2^>(Application); 
     addInInstance = dynamic_cast<AddIn^>(AddInInst); 

     ... 

     // obtain the service provider 
     OLE::Interop::IServiceProvider^ SProvider = safe_cast<OLE::Interop::IServiceProvider^>(appObject); 
     Guid Sol_GuidService = (Guid)(SVsSolution::typeid)->GUID; 
     Guid Sol_riid = (Guid)(SVsSolution::typeid)->GUID; 
     IntPtr Sol_ppvObject; 

     // obtain the solution object 
     if (SProvider->QueryService(Sol_GuidService, Sol_riid, Sol_ppvObject)==VSConstants::S_OK && IntPtr::Zero!=Sol_ppvObject) 
     { 
      IVsSolution^ Sol = safe_cast<IVsSolution^>(Marshal::GetObjectForIUnknown(Sol_ppvObject)); 
      IEnumHierarchies^ EnumHierarchies = nullptr; 
      Guid ProjectGUID = Guid("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"); 

      // enumerate through the projects and bind the project changed events 
      if (Sol->GetProjectEnum((unsigned int)(__VSENUMPROJFLAGS::EPF_MATCHTYPE | __VSENUMPROJFLAGS::EPF_ALLPROJECTS), ProjectGUID, EnumHierarchies)==VSConstants::S_OK && EnumHierarchies!=nullptr) 
      { 
       UInt32 pceltFetched; 
       array<IVsHierarchy^>^ rgelt = gcnew array<IVsHierarchy^>(1){nullptr}; 
       for (EnumHierarchies->Reset(); EnumHierarchies->Next(1, rgelt, pceltFetched)==VSConstants::S_OK && pceltFetched==1;) 
       { 
        Hierarchy^ NewHierarchy = gcnew Hierarchy(rgelt[0]); 
        Hierarchies->Add(NewHierarchy);       
       } 
      } 
     }   
    } 

    ... 
}; 
相關問題