VBProjectsEvents在哪里?

时间:2015-04-26 20:07:51

标签: c# events com-interop vbe

在C#中使用Microsoft.Vbe.Interop,我可以通过CommandBarEvents访问ReferencesEventsVBE.Events

然而,那些有用的MSDN文档似乎表明,当向VBE添加项目或从VBE中删除项目时,我可以使用VBProjectsEvents来通知我的加载项。这正是我想在这里实现的目标。

我可以在对象浏览器中看到_VBProjectsEvents接口,但没有实现它(与_CommandBarControlsEvents接口相反,CommandBarEventsClass接口由{{1}实现使用ReSharper的转到实现功能。

在任何地方都有_VBProjectsEvents接口的实现吗?如果没有,那么如何通知从VB中删除VBProject?

1 个答案:

答案 0 :(得分:2)

您需要为这些事件创建接收器

实现const wchar_t sql[]调度接口 - 这是一个通过调用常规托管.net事件来响应这些事件的实现,有效地“包装”_dispVBProjectsEvents事件:

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项的一种便捷方式,可以重复用于其他接收器:

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 Cookie:

int

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方法“连接”您的接收器并检索 Cookie

Advise

然后你可以处理接收器事件,就像处理任何“普通”.net事件一样:

_projectsEventsConnectionPoint.Advise(sink, out _projectsEventsCookie);

如果要断开接收器,请将 cookie 传递给sink.ProjectAdded += sink_ProjectAdded; sink.ProjectRemoved += sink_ProjectRemoved; sink.ProjectActivated += sink_ProjectActivated; sink.ProjectRenamed += sink_ProjectRenamed; 实例的Unadvise方法:

IConnectionPoint

“那么简单!”