gpt4 book ai didi

c# - VBProjectsEvents 在哪里?

转载 作者:太空狗 更新时间:2023-10-29 21:53:46 26 4
gpt4 key购买 nike

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

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

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

是否有 _VBProjectsEvents 接口(interface)的实现?如果不是,那么如何才能收到从 IDE 中删除的 VBProject 的通知?

最佳答案

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

实现 _dispVBProjectsEvents 调度接口(interface) - 这是一个通过调用常规托管 .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 字段及其 int cookie:

private readonly IConnectionPoint _projectsEventsConnectionPoint;
private readonly int _projectsEventsCookie;

VBProjects 集合实现了IConnectionPointContainer 接口(interface),您需要使用它来找到连接点:

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 传递给 IConnectionPoint 实例的 Unadvise 方法:

_projectsEventsConnectionPoint.Unadvise(_projectsEventsCookie);

“就这么简单!”

关于c# - VBProjectsEvents 在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29882916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com