简体   繁体   English

如何在VSPackage中获取IVsExpansionClient的实例?

[英]How do I get an instance of IVsExpansionClient in VSPackage?

Documentation for extending Visual Studio is virtually nonexistent, and I managed to assemble a few lines of functioning code hacked together from a dozen or more obscure sources around the interwebs before crashing into a brick wall. 几乎没有扩展Visual Studio的文档,我设法将几行功能代码从网络中的十几个或更多晦涩的源代码中窃取到一起,然后撞到砖墙上。

All I need to do is subscribe to an event that is fired after a snippet is inserted. 我要做的就是订阅插入代码段后触发的事件。 Sounds easy enough, or so I thought. 听起来很容易,或者我想。 Almost immediately into my research I stumbled upon the following morsel: 几乎在我的研究中,我偶然发现了以下问题:

IVsExpansionClient.OnAfterInsertion IVsExpansionClient.OnAfterInsertion

That describes perfectly my intention, so this MUST be the answer! 完美地描述了我的意图,所以这一定是答案! After looking around my SDK assemblies for longer than I would like to admit, I finally ended up yanking the assembly (Microsoft.VisualStudio.TextManager.Interop.8.0.dll) out of my GAC so that I could reference it explicitly in my solution. 在对我的SDK程序集进行了长时间的浏览之后,我终于把该程序集(Microsoft.VisualStudio.TextManager.Interop.8.0.dll)从GAC中移出,以便可以在解决方案中明确引用它。

Now that I have access to the interface, I just need to figure out how to get an instance of it. 现在,我可以访问该接口了,我只需要弄清楚如何获取它的一个实例。

IVsExpansionManager IVsExpansionManager

Ah HA! 啊哈! That MUST somehow provide me with a mechanism for obtaining an instance of IVsExpansionClient, right? 那一定以某种方式为我提供了一种获取IVsExpansionClient实例的机制,对吗? Well, not exactly. 好吧,不完全是。 At least not in a way that I can see. 至少没有我能看到的方式。 I have stitched together the following code: 我将以下代码拼接在一起:

IVsExpansionManager expansionManager = null; IVsExpansionManager expandManager = null; IVsTextManager2 textManager = (IVsTextManager2)Package.GetGlobalService(typeof(SVsTextManager)); IVsTextManager2 textManager =(IVsTextManager2)Package.GetGlobalService(typeof(SVsTextManager)); int manager = textManager.GetExpansionManager(out expansionManager); int manager = textManager.GetExpansionManager(outexpansionManager);

Which gives me a IVsExpansionManager COM object, but that is about as far as I can get. 这给了我一个IVsExpansionManager COM对象,但这是我所能达到的。

I have taken notice of another potential provider: 我注意到了另一个潜在的提供商:

IVsExpansionEvents IVsExpansionEvents

I thought perhaps like solution events, document events, window events or text events, I might be able to invoke DTE to hook these events, but they appear to be absent. 我认为也许像解决方案事件,文档事件,窗口事件或文本事件一样,我可能能够调用DTE来挂接这些事件,但似乎不存在。

There are methods that accept IVsExpansionClient as a parameter such as: 有一些方法可以将IVsExpansionClient接受为参数,例如:

IVsExpansion.InsertNamedExpansion IVsExpansion.InsertNamedExpansion

So there simply must be a way to fetch an instance of this object, right? 因此,必须仅存在一种获取该对象的实例的方法,对吗? I suppose it's possible to create my own class that implements the IVsExpansionClient interface, but I wouldn't know where to begin with implementing the interface members, and I still wouldn't know how to instantiate it in a meaningful way within my package. 我想可以创建自己的类来实现IVsExpansionClient接口,但是我不知道从哪里开始实现接口成员,而且我仍然不知道如何在包中以有意义的方式实例化它。

I am grateful to anyone who can point me in a different direction, or provide a working sample that I can adapt for my needs. 我感谢任何可以将我指向不同的方向或提供可以满足我的需求的工作样本的人。 Thanks for taking the time to read through this, I appreciate your time. 感谢您抽出宝贵的时间阅读本文档,感谢您的宝贵时间。

EDIT: I want to receive a notification that a snippet has been inserted into the active document window. 编辑:我想收到一个通知,该通知已将片段插入到活动文档窗口中。 Ideally, I would like the snippet itself to be included in the delegate event args, as I have some processing to do on the inserted snippet...and it would be cumbersome to process the entire document, or try to identify the recently inserted snippet without context. 理想情况下,我希望将代码段本身包含在委托事件args中,因为我需要对插入的代码段进行一些处理...而且处理整个文档或尝试识别最近插入的代码段将很麻烦没有上下文。

Maybe you want to explain what you actually want to achieve. 也许您想解释一下您实际想要实现的目标。 The documentation of the IVsExpansionClient interface states: IVsExpansionClient接口的文档指出:

Notes to implementers This interface is implemented by a VSPackage that supports insertion of code snippets. 对实现者的说明此接口由支持插入代码段的VSPackage实现。

I don´t see why one would like to consume an instance of it, because it´s an interface allowing the package to receive notifications/callbacks, if something related to code-snippets is going to happen (it provides nohting else than callback functions). 我不明白为什么要使用它的一个实例,因为它是一个接口,允许包在发生与代码片段相关的事情时接收通知/回调(它提供了回调函数以外的其他功能) )。

It states furthermore... 它进一步指出...

Notes to Callers This interface is instantiated and passed to the InvokeInsertionUI method in the IVsExpansionManager interface. InvokeInsertionUI 者的说明此接口已实例化并传递到IVsExpansionManager接口中的InvokeInsertionUI方法。 This interface is also instantiated and passed to the methods in the IVsExpansion interface. 该接口也被实例化并传递给IVsExpansion接口中的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM