在我的 Visual Studio 包上的服务器资源管理器窗口中添加或删除新连接后,我尝试对菜单进行一些更改。因此,我正在寻找可以订阅的事件或任何其他方式来允许我检测何时添加新的连接节点。有没有人做过类似的事情? 非常感谢任何提示。
最佳答案
服务器资源管理器是一个类似于解决方案资源管理器的 IVsUIHierarchy
。我还没有尝试过,但您应该通过请求 IVsServerExplorer
实例来访问服务器资源管理器层次结构,例如:
using Microsoft.VSDesigner.ServerExplorer;
...
IVsServerExplorer explorer = (IVsServerExplorer) package.GetGlobalService(typeof(IVsServerExplorer));
IVsUIHierarchy hierarchy;
if ((hierarchy = explorer as IVsUIHierarchy) != null)
{
...
}
一旦你有了层次结构,你就可以订阅通知...
IVsHierarchyEvents eventSink = new ...
uint cookie = 0;
int result = hierarchy.AdviseHierarchyEvents(eventSink, out cookie);
if (result != VSConstants.S_OK)
{
ErrorHandler.ThrowOnFailure(result);
}
您需要提供一个IVsHierarchyEvents
实例;该对象将接收通知。我建议将该接口(interface)实现为监听器类,该类可以正确处理建议和取消建议(您需要保留 cookie,以便取消订阅事件通知)。
class HierarchyEventsListener : IVsHierarchyEvents, IDisposable
{
private readonly IVsUIHierarchy hierarchy;
private uint cookie;
protected HierarchyEventsListener(IVsUIHierarchy hierarchy)
{
this.hierarchy = hierarchy;
int hr = this.hierarchy.AdviseHierarchyEvents(this, out cookie);
ErrorHandler.ThrowOnFailure(hr);
}
public int OnItemAdded(uint itemidParent, uint itemidSiblingPrev, uint itemidAdded)
{
...
}
...
public void Dispose()
{
if (this.cookie != 0)
{
this.hierarchy.UnadviseHierarchyEvents(this.cookie);
this.cookie = 0;
}
}
}
在您的情况下,您可能对 OnItemAdded
和/或 OnItemsAppend
方法感兴趣,该方法允许您查询新添加的项目...
public int OnItemAdded(uint itemidParent, uint itemidSiblingPrev, uint itemidAdded)
{
const int Property = (int)__VSHPROPID.VSHPROPID_Caption; // let´s ask for the caption (for instance)
object value;
int hr = this.hierarchy.GetProperty(itemidAdded, Property, out value);
if (hr == VSConstants.S_OK)
{
...
}
return VSConstants.S_OK;
}
也许您需要使用它来弄清楚添加的项目是否是连接节点(猜测有类型属性等),或者不是。
关于visual-studio-extensions - 获取 VS 包内服务器资源管理器窗口的添加连接事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21925167/