当前位置: 首页 > 编程语言 > C#

VSIX:获取DTE对象共享

时间:2023-04-11 01:14:23 C#

VSIX:获取DTE对象我的VisualStudio包需要使用EnvDTE.DTE变量,但它始终返回null。在阅读了许多hack之后,所有人都说使用OnShellPropertyChange()方法(IVsShellPropertyEvents),但有时它永远不会触发-就好像我的扩展永远不会完成加载一样。我正在使用VS2010并检查了VSSPROPID_Zombie和ShellInitialized-没有用。?有任何想法吗?这是我正在使用的代码:publicintOnShellPropertyChange(intpropid,objectvar){if(propid==-9053||(int)__VSSPROPID.VSSPROPID_Zombie==propid){//-9053=ShellInittry{if((bool)var==false){Dte=GetService(typeof(SDTE))asDTE;流量.Dte=Dte;varshellService=GetService(typeof(SVsShell))asIVsShell;如果(shellService!=null)ErrorHandler.ThrowOnFailure(shellService.UnadviseShellPropertyChanges(_cookie));_cookie=0;}}catch{}}返回VSConstants.S_OK;编辑:在实验实例下,它工作正常,初始化大约需要5秒。但是,一旦部署为VSIX-它根本不会触发。我在这里看到几个问题:试试这个命令:dte=Package.GetGlobalService(typeof(DTE))asDTE2;如果你有MEF组件,获取DTE对象的最简单方法如下首先添加对Microsoft.VisualStudio.Shell的引用对.Immutable.10的引用。然后为SVsServiceProvider添加MEF导入。这个对象有一个查询DTE的GetService方法[ImportingConstructor]publicMyComponent(SVsServiceProviderserviceProvider){_DTEdte=(_DTE)serviceProvider.GetService(typeof(_DTE));我知道你已经选择了一个答案,但你确实指出你不想使用MEF所以我想我会发布一个替代方案只是为了完整性....;p以上是C#学习教程:VSIX:如果对任何人都有用并且需要了解更多有关C#的信息,请获取DTE对象共享的完整内容UseSystem.Collections.Generic;使用System.Linq;使用系统文本;使用System.Runtime.InteropServices;使用EnvDTE;使用EnvDTE80;namespaceDTETesting{classProgram{conststringACTIVE_OBJECT="VisualStudio.DTE.10.0";staticvoidMain(string[]args){EnvDTE80.DTE2MyDte;MyDte=(EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject(ACTIVE_OBJECT);Console.WriteLine("版本是"+MyDte.Edition);控制台.ReadLine();}}}使用系统;使用System.Collections.Generic;使用System.Linq;使用系统文本;使用System.Runtime.InteropServices;使用EnvDTE;使用EnvDTE80;命名空间DTETesting{类s程序{conststringACTIVE_OBJECT="VisualStudio.DTE.10.0";staticvoidMain(string[]args){EnvDTE80.DTE2MyDte;MyDte=(EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject(ACTIVE_OBJECT);Console.WriteLine("版本是"+MyDte.Edition);控制台.ReadLine();}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: