[英]Visual Studio Extension: Get Package from adornment
I'm developing a Visual Studio Extension, made up of: 我正在开发一个Visual Studio扩展,它由以下组成:
Now, while the Tools Window and the commands are either wired up by, or have a handle on, the AsyncPackage for my extension, what I cannot figure out is HOW I get a handle to the self-same AsyncPackage from one or more of my text adornments. 现在,虽然“工具”窗口和命令或者通过我的扩展程序的AsyncPackage进行连接或使用了句柄,但我不知道是如何从一个或多个我的同一个AsyncPackage中获得句柄的文字装饰。
For example, my Tools Window extends ToolWindowPane, which has a hook to the Package via the Package's ProvideToolWindow attribute. 例如,我的“工具窗口”扩展了ToolWindowPane,它通过Package的ProvideToolWindow属性与Package挂钩。 My commands are constructed inside the Package itself, so passing a handle to the AsyncPackage is simple enough.
我的命令是在Package本身内部构造的,因此将句柄传递给AsyncPackage非常简单。
What I cannot work out is HOW you get a reference to this AsyncPackage inside any of my TextAdornments. 我无法解决的是如何在我的任何TextAdornments中获得对此AsyncPackage的引用。
Any help? 有什么帮助吗?
This was a tricky one! 这是一个棘手的问题! You must get the IVsShell to retrieve a package based on a GUID you associate with your Package, and then cast it to your interface (or the base interface of IPackage)
您必须获取IVsShell才能基于与该Package关联的GUID检索一个Package,然后将其转换为您的接口(或IPackage的基本接口)
private IMyPackageInterface _myPackage;
//let's get our hands on that package
var vsShell = (IVsShell) ServiceProvider.GlobalProvider.GetService(typeof(IVsShell));
if (vsShell == null)
{
throw new NullReferenceException();
}
if (vsShell.IsPackageLoaded(PackageGuid, out var myPossiblePackage)
== Microsoft.VisualStudio.VSConstants.S_OK) {
_myPackage = (IMyPackageInterface)myPossiblePackage;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.