ProjectItemsEvents.ItemAdded:如何发现项目类型?
问题描述:
我开始开发一个VSPackage,想知道什么时候将新引用添加到项目中,但是订阅ProjectItemsEvents的ItemAdded会为我提供一个ProjectItem对象,而且我不知道如何检查它是否是引用。有人可以帮我解决这个问题吗?提前致谢。ProjectItemsEvents.ItemAdded:如何发现项目类型?
答
幸运的是,我发现类VSLangProj.ReferencesEvents触发了我需要的ReferenceAdded事件。所以我做了:
private static ReferencesEvents _refEvents;
private static Events2 _dteEvents;
public void SubscribeEvents()
{
EnvDTE.DTE dte = (EnvDTE.DTE)ServiceProvider.GetService(typeof(EnvDTE.DTE));
_dteEvents = dte.Events as Events2;
_refEvents = (ReferencesEvents)_dteEvents.GetObject("CSharpReferencesEvents");
_refEvents.ReferenceAdded += new _dispReferencesEvents_ReferenceAddedEventHandler(ReferenceAdded);
}
private void ReferenceAdded(Reference pReference)
{
// do some stuff.
}