VBProjectsEvents 在哪里?
Where are the VBProjectsEvents?
在 C# 中使用 Microsoft.Vbe.Interop
,我可以通过 VBE.Events
.
访问 CommandBarEvents
和 ReferencesEvents
然而,非常有用的 MSDN 文档似乎表明有一个 VBProjectsEvents
可以用来在添加或删除项目时通知我的加载项 to/from VBE...这正是我要在这里实现的目标。
我可以在 对象浏览器 中看到 _VBProjectsEvents
接口,但没有实现它(与实现的 _CommandBarControlsEvents
接口相反通过 CommandBarEventsClass
),使用 ReSharper 的 go to implementation 功能。
是否有 _VBProjectsEvents
接口的实现?如果不是,那么如何才能收到从 IDE 中删除 VBProject 的通知?
您需要为这些事件创建一个接收器。
实施 _dispVBProjectsEvents
调度接口 - 这是一个通过调用常规托管 .net 事件响应这些事件的实施,有效地 "wrapping" VBProjects
事件:
public class VBProjectsEventsSink : _dispVBProjectsEvents
{
public event EventHandler<DispatcherEventArgs<VBProject>> ProjectAdded;
public void ItemAdded(VBProject VBProject)
{
if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none)
{
OnDispatch(ProjectAdded, VBProject);
}
}
public event EventHandler<DispatcherEventArgs<VBProject>> ProjectRemoved;
public void ItemRemoved(VBProject VBProject)
{
if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none)
{
OnDispatch(ProjectRemoved, VBProject);
}
}
public event EventHandler<DispatcherRenamedEventArgs<VBProject>> ProjectRenamed;
public void ItemRenamed(VBProject VBProject, string OldName)
{
var handler = ProjectRenamed;
if (handler != null && VBProject.Protection == vbext_ProjectProtection.vbext_pp_none)
{
handler.Invoke(this, new DispatcherRenamedEventArgs<VBProject>(VBProject, OldName));
}
}
public event EventHandler<DispatcherEventArgs<VBProject>> ProjectActivated;
public void ItemActivated(VBProject VBProject)
{
if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none)
{
OnDispatch(ProjectActivated, VBProject);
}
}
private void OnDispatch(EventHandler<DispatcherEventArgs<VBProject>> dispatched, VBProject project)
{
var handler = dispatched;
if (handler != null)
{
handler.Invoke(this, new DispatcherEventArgs<VBProject>(project));
}
}
}
DispatcherEventArgs
class 只是一种方便的方式来公开与事件相关的 VBProject
项目,它可以被其他接收器重复使用:
public class DispatcherEventArgs<T> : EventArgs
where T : class
{
private readonly T _item;
public DispatcherEventArgs(T item)
{
_item = item;
}
public T Item { get { return _item; } }
}
客户端代码需要注册接收器 - 为此您需要保留一个 IConnectionPoint
字段及其 int
cookie:
private readonly IConnectionPoint _projectsEventsConnectionPoint;
private readonly int _projectsEventsCookie;
VBProjects
集合实现了IConnectionPointContainer
接口,需要用到找到连接点:
var sink = new VBProjectsEventsSink();
var connectionPointContainer = (IConnectionPointContainer)_vbe.VBProjects;
var interfaceId = typeof (_dispVBProjectsEvents).GUID;
connectionPointContainer.FindConnectionPoint(ref interfaceId, out _projectsEventsConnectionPoint);
获得 IConnectionPoint
后,使用 Advise
方法 "connect" 您的接收器并检索 cookie:
_projectsEventsConnectionPoint.Advise(sink, out _projectsEventsCookie);
然后您可以像处理任何 "normal" .net 事件一样处理接收事件:
sink.ProjectAdded += sink_ProjectAdded;
sink.ProjectRemoved += sink_ProjectRemoved;
sink.ProjectActivated += sink_ProjectActivated;
sink.ProjectRenamed += sink_ProjectRenamed;
当你想断开你的接收器时,你将 cookie 传递给 IConnectionPoint
实例的 Unadvise
方法:
_projectsEventsConnectionPoint.Unadvise(_projectsEventsCookie);
"Simple as that!"
在 C# 中使用 Microsoft.Vbe.Interop
,我可以通过 VBE.Events
.
CommandBarEvents
和 ReferencesEvents
然而,非常有用的 MSDN 文档似乎表明有一个 VBProjectsEvents
可以用来在添加或删除项目时通知我的加载项 to/from VBE...这正是我要在这里实现的目标。
我可以在 对象浏览器 中看到 _VBProjectsEvents
接口,但没有实现它(与实现的 _CommandBarControlsEvents
接口相反通过 CommandBarEventsClass
),使用 ReSharper 的 go to implementation 功能。
是否有 _VBProjectsEvents
接口的实现?如果不是,那么如何才能收到从 IDE 中删除 VBProject 的通知?
您需要为这些事件创建一个接收器。
实施 _dispVBProjectsEvents
调度接口 - 这是一个通过调用常规托管 .net 事件响应这些事件的实施,有效地 "wrapping" VBProjects
事件:
public class VBProjectsEventsSink : _dispVBProjectsEvents
{
public event EventHandler<DispatcherEventArgs<VBProject>> ProjectAdded;
public void ItemAdded(VBProject VBProject)
{
if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none)
{
OnDispatch(ProjectAdded, VBProject);
}
}
public event EventHandler<DispatcherEventArgs<VBProject>> ProjectRemoved;
public void ItemRemoved(VBProject VBProject)
{
if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none)
{
OnDispatch(ProjectRemoved, VBProject);
}
}
public event EventHandler<DispatcherRenamedEventArgs<VBProject>> ProjectRenamed;
public void ItemRenamed(VBProject VBProject, string OldName)
{
var handler = ProjectRenamed;
if (handler != null && VBProject.Protection == vbext_ProjectProtection.vbext_pp_none)
{
handler.Invoke(this, new DispatcherRenamedEventArgs<VBProject>(VBProject, OldName));
}
}
public event EventHandler<DispatcherEventArgs<VBProject>> ProjectActivated;
public void ItemActivated(VBProject VBProject)
{
if (VBProject.Protection == vbext_ProjectProtection.vbext_pp_none)
{
OnDispatch(ProjectActivated, VBProject);
}
}
private void OnDispatch(EventHandler<DispatcherEventArgs<VBProject>> dispatched, VBProject project)
{
var handler = dispatched;
if (handler != null)
{
handler.Invoke(this, new DispatcherEventArgs<VBProject>(project));
}
}
}
DispatcherEventArgs
class 只是一种方便的方式来公开与事件相关的 VBProject
项目,它可以被其他接收器重复使用:
public class DispatcherEventArgs<T> : EventArgs
where T : class
{
private readonly T _item;
public DispatcherEventArgs(T item)
{
_item = item;
}
public T Item { get { return _item; } }
}
客户端代码需要注册接收器 - 为此您需要保留一个 IConnectionPoint
字段及其 int
cookie:
private readonly IConnectionPoint _projectsEventsConnectionPoint;
private readonly int _projectsEventsCookie;
VBProjects
集合实现了IConnectionPointContainer
接口,需要用到找到连接点:
var sink = new VBProjectsEventsSink();
var connectionPointContainer = (IConnectionPointContainer)_vbe.VBProjects;
var interfaceId = typeof (_dispVBProjectsEvents).GUID;
connectionPointContainer.FindConnectionPoint(ref interfaceId, out _projectsEventsConnectionPoint);
获得 IConnectionPoint
后,使用 Advise
方法 "connect" 您的接收器并检索 cookie:
_projectsEventsConnectionPoint.Advise(sink, out _projectsEventsCookie);
然后您可以像处理任何 "normal" .net 事件一样处理接收事件:
sink.ProjectAdded += sink_ProjectAdded;
sink.ProjectRemoved += sink_ProjectRemoved;
sink.ProjectActivated += sink_ProjectActivated;
sink.ProjectRenamed += sink_ProjectRenamed;
当你想断开你的接收器时,你将 cookie 传递给 IConnectionPoint
实例的 Unadvise
方法:
_projectsEventsConnectionPoint.Unadvise(_projectsEventsCookie);
"Simple as that!"