简体   繁体   English

Visual Studio扩展:从装饰品获取包

[英]Visual Studio Extension: Get Package from adornment

I'm developing a Visual Studio Extension, made up of: 我正在开发一个Visual Studio扩展,它由以下组成:

  1. A menu and series of commands 菜单和一系列命令
  2. A tools window 工具窗口
  3. One or more textview adornments 一个或多个textview装饰
  4. A custom implementation of AysncPackage AysncPackage的自定义实现

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.

相关问题 从Visual Studio文本修饰扩展名获取当前文件名 - Get the current filename from a Visual Studio text adornment extension 视觉工作室绘图装饰与懒惰更新 - visual studio drawing adornment with lazy update 将.exe打包到.vsix中,然后从Visual Studio扩展中调用 - Package .exe into .vsix and call from Visual Studio extension 如何从扩展代码中获取 Visual Studio 扩展版本 - How ot get Visual studio extension version from the extension code 是否有任何 NuGet API 可以从 Visual Studio 扩展项目创建包 - Is there any NuGet API to create a Package from a Visual Studio Extension project 如何从Visual Studio Bitbucket扩展获取开发分支 - How to get a development branch from Visual Studio Bitbucket extension 如何从Visual Studio扩展和新的csproj格式获取IntermediateOutputPath - How to get IntermediateOutputPath from Visual Studio extension and new csproj format 从程序集属性中获取 Nuget package 属性(在 Visual Studio 2022 中) - Get Nuget package properties from assembly attributes (in Visual Studio 2022) 如何从 Visual Studio Package 项目中获取当前的解决方案名称? - How to get the current solution name from a Visual Studio Package project? Visual Studio调试器扩展获取用户设置 - Visual Studio Debugger Extension get user settings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM