7

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

然而,非常有用的MSDN文档似乎表明,VBProjectsEvents当项目被添加到 VBE 或从 VBE 中删除时,我可以使用它来通知我的加载项......这正是我在这里想要实现的.

我可以在object browser中看到该_VBProjectsEvents接口,但没有实现它(与由 实现的接口相反),使用 ReSharper 的go to implementation功能。_CommandBarControlsEventsCommandBarEventsClass

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

4

1 回答 1

3

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

实现_dispVBProjectsEvents调度接口 - 这是一个通过调用常规托管 .net 事件来响应这些事件的实现,有效地“包装”这些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字段及其intcookie:

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方法“连接”你的接收器并检索一个cookie

_projectsEventsConnectionPoint.Advise(sink, out _projectsEventsCookie);

然后您可以像处理任何“正常”.net 事件一样处理接收器事件:

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

当你想断开你的接收器时,你将cookie传递给实例的Unadvise方法:IConnectionPoint

_projectsEventsConnectionPoint.Unadvise(_projectsEventsCookie);

“就那么简单!”

于 2016-04-04T14:32:11.347 回答