Where are the VBProjectsEvents?

倖福魔咒の 提交于 2019-12-07 04:40:52

问题


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

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

I can see that _VBProjectsEvents interface in the object browser, but no implementation for it (as opposed to the _CommandBarControlsEvents interface, which is implemented by the CommandBarEventsClass), using ReSharper's go to implementation feature.

Is there an implementation of the _VBProjectsEvents interface anywhere? If not, then how does one go about being notified of a VBProject being removed from the IDE?


回答1:


You need to create a sink for these events.

Implement the _dispVBProjectsEvents dispatch interface - here's an implementation that responds to these events by invoking regular managed .net events, effectively "wrapping" the 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 expose the VBProject item involved with the event, and it can be reused for other sinks:

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 needs to register the sink - and for that you need to keep an IConnectionPoint field and its int cookie:

private readonly IConnectionPoint _projectsEventsConnectionPoint;
private readonly int _projectsEventsCookie;

The VBProjects collection implements the IConnectionPointContainer interface, which you need to use 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 the IConnectionPoint, use the Advise method to "connect" your sink and retrieve a cookie:

_projectsEventsConnectionPoint.Advise(sink, out _projectsEventsCookie);

And then you can handle the sink events as you would any "normal" .net events:

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

When you want to disconnect your sink, you pass the cookie to the Unadvise method of the IConnectionPoint instance:

_projectsEventsConnectionPoint.Unadvise(_projectsEventsCookie);

"Simple as that!"



来源:https://stackoverflow.com/questions/29882916/where-are-the-vbprojectsevents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!