简体   繁体   English

如何通过Visual Studio加载项跟踪项目项的属性更改

[英]How can I track property changes of a project item through visual studio Add-In

I'm developing a Visual Studio 2010 Add-In and I'm stuck at this moment. 我正在开发Visual Studio 2010加载项,但目前仍处于困境。 I would like to receive notification about property changes of specific project items. 我想收到有关特定项目项目的属性更改的通知。 For example I need to know if a new Form (winform) was added into my project. 例如,我需要知道是否在我的项目中添加了新窗体(winform)。

There are 2 possibilities how to add a form into the project in Visual Studio 2010: 如何在Visual Studio 2010中向项目添加表单有两种可能性:

  1. You can simply Add a new Form item through the context menu. 您可以通过上下文菜单简单地添加一个新的Form项。 In this case the ProjectItemAdded event is thrown by the IDE. 在这种情况下,IDE将引发ProjectItemAdded事件。 The problem is, that at that time the new form is recognized as eFileTypeCppHeader and not eFileTypeCppForm . 问题在于,那时新表单被识别为eFileTypeCppHeader而不是eFileTypeCppForm

  2. The second option is to change the FileType property of a Header file to " C++ Form " through the property tab. 第二个选项是通过属性选项卡将标头文件的FileType属性更改为“ C ++ Form ”。 There is no event which notifies the Add-In about this property change. 没有任何事件通知外接程序此属性更改。

So, how can my Add-In know the property of an ProjectItem was changed? 因此,我的加载项如何知道ProjectItem的属性已更改?

Finally I found out how to bind to the project property changed event. 最后,我了解了如何绑定到项目属性更改事件。 It is necessary to implement the IVsHierarchy interface and then implement the OnPropertyChanged event. 这是要落实IVsHierarchy接口,然后实现OnPropertyChanged事件。 It is also necessary to enumerate through the opened projects and bind to their property changes one by one. 还需要枚举已打开的项目,并将它们的属性更改逐一绑定。 See the example bellow. 请参见下面的示例。

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

    ...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在哪里可以找到Visual Studio的“ Outlook 2016加载项”? - Where can I find “Outlook 2016 Add-in” for Visual Studio? 如何在Visual Studio网站项目类型中添加引用? - How can I add a reference in a Visual Studio website project type? 如何将 jQuery DateTimePicker 添加到我在 visual studio 中的项目 - How can I add jQuery DateTimePicker to my project in visual studio 无法设置断点来调试Visual Studio中的外接程序项目 - Unable to set a breakpoint to debug an add-in project in Visual Studio $(TargetDir)对于Visual Studio 2017中的Microsoft Word加载项项目为空 - $(TargetDir) empty for Microsoft Word Add-In project in Visual Studio 2017 如何以编程方式将Visual Studio加载项窗口附加到选项卡? - How do I attach my Visual Studio Add-in window to a tab programmatically? 如何在Visual Studio中使用外接程序库F5部署? - How to F5 deploy with add-in libraries in Visual Studio? Visual Studio 2012加载项 - 如何将调试器附加到进程 - Visual studio 2012 add-in - how to attach debugger to a process 如何将Lucene.Net依赖项添加到Visual Studio项目 - How can I add a Lucene.Net dependency to a Visual Studio project 如何在我的 C# Visual Studio 项目中添加 VC++ DLL 作为参考? - How can I add a VC++ DLL as a reference in my C# Visual Studio project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM