Where is VBProjectsEvents?

Using Microsoft.Vbe.Interop in C #, I can access CommandBarEvents and ReferencesEvents through VBE.Events .

However, the useful MSDN documentation seems to indicate that there are VBProjectsEvents that I could use to notify my add- when a project is added or removed to / from VBE ... which is exactly what I'm trying to achieve here.

I see the _VBProjectsEvents interface in the object browser, but there is no implementation for it (unlike the _CommandBarControlsEvents interface, which is implemented by CommandBarEventsClass ) using ReSharper to implement.

Is there an implementation of the _VBProjectsEvents interface anywhere? If not, how can I get a notification that VBProject has been removed from the IDE?

+6
source share
1 answer

You need to create a receiver for these events.

Implementing the _dispVBProjectsEvents dispatch _dispVBProjectsEvents is an implementation that responds to these events by triggering regular .net events, effectively wrapping VBProjects events:

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

The DispatcherEventArgs class is just a convenient way to show the VBProject element associated with the event and can be reused for other receivers:

 public class DispatcherEventArgs<T> : EventArgs where T : class { private readonly T _item; public DispatcherEventArgs(T item) { _item = item; } public T Item { get { return _item; } } } 

The client code must register the receiver - and for this you need to save the IConnectionPoint field and its int cookie:

 private readonly IConnectionPoint _projectsEventsConnectionPoint; private readonly int _projectsEventsCookie; 

The VBProjects collection implements the IConnectionPointContainer interface, which must be used to find the connection point:

 var sink = new VBProjectsEventsSink(); var connectionPointContainer = (IConnectionPointContainer)_vbe.VBProjects; var interfaceId = typeof (_dispVBProjectsEvents).GUID; connectionPointContainer.FindConnectionPoint(ref interfaceId, out _projectsEventsConnectionPoint); 

Once you have IConnectionPoint , use the Advise method to β€œconnect” your receiver and receive a cookie:

 _projectsEventsConnectionPoint.Advise(sink, out _projectsEventsCookie); 

And then you can handle the events of the receiver, like any "regular" .net events:

 sink.ProjectAdded += sink_ProjectAdded; sink.ProjectRemoved += sink_ProjectRemoved; sink.ProjectActivated += sink_ProjectActivated; sink.ProjectRenamed += sink_ProjectRenamed; 

If you want to disconnect your receiver, you pass the cookie to the Unadvise method of the Unadvise instance:

 _projectsEventsConnectionPoint.Unadvise(_projectsEventsCookie); 

"Just!"

+2
source

Source: https://habr.com/ru/post/986008/


All Articles