Mat*_*don 6 c# events com-interop vbe
Microsoft.Vbe.Interop在C#中使用,我可以访问CommandBarEvents和ReferencesEvents通过VBE.Events.
然而,那些非常有用的MSDN文档似乎表明,VBProjectsEvents当向VBE添加项目或从VBE中删除项目时,我可以使用它来通知我的加载项...这正是我在这里想要实现的.
我可以_VBProjectsEvents在对象浏览器中看到该接口,但没有实现它(而不是_CommandBarControlsEvents接口,由它实现CommandBarEventsClass),使用ReSharper的实现功能.
在_VBProjectsEvents任何地方都有接口的实现吗?如果没有,那么如何通知从VB中删除VBProject?
您需要为这些事件创建一个接收器。
实现_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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
该类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; } }
}
Run Code Online (Sandbox Code Playgroud)
客户端代码需要注册接收器 - 为此,您需要保留一个IConnectionPoint字段及其intcookie:
private readonly IConnectionPoint _projectsEventsConnectionPoint;
private readonly int _projectsEventsCookie;
Run Code Online (Sandbox Code Playgroud)
该VBProjects集合实现了IConnectionPointContainer接口,您需要使用该接口来查找连接点:
var sink = new VBProjectsEventsSink();
var connectionPointContainer = (IConnectionPointContainer)_vbe.VBProjects;
var interfaceId = typeof (_dispVBProjectsEvents).GUID;
connectionPointContainer.FindConnectionPoint(ref interfaceId, out _projectsEventsConnectionPoint);
Run Code Online (Sandbox Code Playgroud)
一旦你有了IConnectionPoint,使用该Advise方法“连接”你的接收器并检索cookie:
_projectsEventsConnectionPoint.Advise(sink, out _projectsEventsCookie);
Run Code Online (Sandbox Code Playgroud)
然后您可以像处理任何“正常”.net 事件一样处理接收器事件:
sink.ProjectAdded += sink_ProjectAdded;
sink.ProjectRemoved += sink_ProjectRemoved;
sink.ProjectActivated += sink_ProjectActivated;
sink.ProjectRenamed += sink_ProjectRenamed;
Run Code Online (Sandbox Code Playgroud)
当您想要断开接收器的连接时,可以将cookie传递给实例Unadvise的方法IConnectionPoint:
_projectsEventsConnectionPoint.Unadvise(_projectsEventsCookie);
Run Code Online (Sandbox Code Playgroud)
“就那么简单!”
| 归档时间: |
|
| 查看次数: |
141 次 |
| 最近记录: |