Visual Studio SDK-处理文件添加,删除和重命名事件 [英] Visual Studio SDK - Handling File Add, Remove, and Rename Events

查看:100
本文介绍了Visual Studio SDK-处理文件添加,删除和重命名事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Visual Studio扩展,当用户在当前解决方案中添加,删除或重命名文件时,该扩展应侦听事件.

I'm working on a Visual Studio extension that should listen for events when the user adds, removes, or renames files in the current solution.

此问题的答案指出VS为通过 DocumentEvents听文档事件,例如保存,打开和关闭界面.例如:

The answer to this question notes that VS provides infrastructure for listening to document events like saving, opening and closing through the DocumentEvents interface. For example:

Dte.Events.DocumentEvents.DocumentSaved

是否有类似的事件可以让我听用户添加/删除/重命名文档?

Are there similar events that would allow me to listen to the user adding/removing/renaming documents?

推荐答案

首先,如果可以帮助,请不要使用DTE.这是一个非常不完整的,摇摇欲坠的抽象,记录在一个极其复杂的界面上.话虽如此,但我承认有时候这非常方便,因为如果没有它(稀有)或替代代码将相当长(很少见),那么等效操作将无法完成.

First, don't use DTE if you can help it. It's a very incomplete, shaky abstraction papered over an extremely complex interface. Having said that, I admit that sometimes it's super handy because the equivalent either can't be done without it (rare) or the alternate code would be quite long (less rare).

这里有两个概念被合并.第一个是运行文档表(RDT). RDT代表所有打开的文件(包括打开的.sln和项目文件).您可以订阅RDT事件,以通知它们正在打开,关闭,重命名等文件.但是这些事件仅适用于打开的文件!

There are two concepts being conflated here. The first is the Running Document Table (RDT). The RDT represents all the open files (including the open .sln and project files). You can subscribe to RDT events to be notified of files being opened, closed, renamed, etc. But these events are for open files only!

第二个概念是项目系统.解决方案资源管理器中加载并显示的每个项目均由该项目类型的项目系统加载. C ++项目,C#项目,F#项目,WIX安装程序项目等都有不同的项目系统.甚至可以有扩展实现的自定义项目系统.听起来您想了解项目系统中的事件,而不是(仅)打开文件的事件.因此,您的重点是项目系统.但是,由于所有项目系统都有不同的实现,因此这变得非常棘手. VS正朝着通用项目系统(CPS)迈进,但目前还不是100%,即使存在,也仍然存在所有旧版扩展等问题.

The second concept is the project system. Each project loaded and displayed in the solution explorer is loaded by the project system for that project's type. C++ projects, C# projects, F# projects, WIX installer projects, etc. all have different project systems. There can even be custom project systems implemented by extensions. It sounds like you want to know about events in the project system, and not events for (just) open files. So your focus is the project system. However, since all project systems have different implementations, this becomes very tricky. VS is moving towards a common project system (CPS), but it's not 100% there yet, and even when it is there remains the problem of all the legacy extensions, etc.

您可以订阅所有项目系统都必须提供的常规层次结构"事件.例如,它们会告诉您何时添加或删除文件(实际上是何时添加或删除层次结构项(节点),因为文件和层次结构项之间不一定存在对应关系).还有一个事件表明整个层次结构已失效-这是一种刷新,您必须丢弃对项目了解的所有信息并收集新信息.

You can subscribe to general "hierarchy" events which all project systems must furnish. They'll tell you for example when a file is added or removed (really, when a hierarchy item (node) is added or removed, since there's not necessarily a correspondence between files and hierarchy items). There's also an event that says the entire hierarchy has been invalidated -- a sort of refresh where you have to discard everything you know about the project and gather up new info.

重命名可能是最难检测到的东西.每个项目系统都以不同的方式实现它.在某些项目系统中,重命名会将自己显示为节点删除,然后再添加节点,而没有可靠的方法来确定它是由于重命名造成的.

Rename is probably the hardest thing to detect. Every project system implements it differently. In some project systems, a rename will present itself as a node deletion followed by a node addition, with no solid way to identify that it was due to a rename.

总而言之,没有什么看起来简单,特别是涉及项目系统(Visual Studio扩展性最差的部分之一)时.您可能最终会获得特定于一个或少数几个项目系统的代码,但无法在全球范围内正常工作. (毕竟,并非所有项目都代表文件层次结构!那些仍然具有文件夹,特殊参考节点等不是文件的项目.)

To sum up, nothing is as simple as it seems, particularly when it comes to project systems (one of the least extensible parts of Visual Studio). You'll likely end up with code that is specific to one or a handful of project systems, but won't work universally. (After all, not all projects even represent file hierarchies! And those that do still have folders, special reference nodes, etc. that aren't files.)

一些正确方向的具体指针:

Some concrete pointers in the right direction:

  • 在要加载/卸载项目的通知中执行IVsSolutionEvents3(并在要重命名项目本身的通知中通知IVsSolutionEvents4).通过SVsSolutionBuildManager服务(广播到IVsSolutionBuildManager3并在其上调用AdviseUpdateSolutionEvents3)将该对象注册为包初始化代码中的侦听器(确保在打开解决方案之前已加载您的包).
  • 实施IVsHierarchyEvents来通知项目更改,例如节点属性更改(使用__VSHPROPID枚举来查找哪个),节点被添加,移除,失效等.在AdviseHierarchyEvents >对象传递给IVsSolutionEvents3OnAfterProjectOpen实现,以注册事件侦听器对象.
  • Implement IVsSolutionEvents3 to be notified of a project being loaded/unloaded (and IVsSolutionEvents4 to be notified of a project itself being renamed). Register that object as a listener in your package initialization code (make sure your package is loaded before a solution is opened) via the SVsSolutionBuildManager service (cast to IVsSolutionBuildManager3 and call AdviseUpdateSolutionEvents3 on it).
  • Implement IVsHierarchyEvents to be notified of project changes like node properties changing (use the __VSHPROPID enum to find out which is which), nodes being added, removed, invalidated, etc. Call AdviseHierarchyEvents on the IVsHierarchy object passed to the IVsSolutionEvents3's OnAfterProjectOpen implementation to register the event listener object.

这篇关于Visual Studio SDK-处理文件添加,删除和重命名事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆