我的Visual Studio包需要使用EnvDTE.DTE变量,但它总是返回空值。在阅读了许多技巧之后,所有这些技巧都建议使用OnShellPropertyChange()方法(IVsShellPropertyEvents),但有时它永远不会触发-就好像我的扩展永远不会完成加载一样。
我正在使用VS2010,并同时检查VSSPROPID_Zombie和ShellInitialized --没有工作。:(
有什么想法吗?这是我使用的代码:
public int OnShellPropertyChange(int propid, object var) {
if (propid == -9053 || (int) __VSSPROPID.VSSPROPID_Zombie == propid) { // -9053 = ShellInit
try {
if ((bool) var == false) {
Dte = GetService(typeof (SDTE)) as DTE;
Flow.Dte = Dte;
var shellService = GetService(typeof (SVsShell)) as IVsShell;
if (shellService != null)
ErrorHandler.ThrowOnFailure(shellService.UnadviseShellPropertyChanges(_cookie));
_cookie = 0;
}
} catch {
}
}
return VSConstants.S_OK;
}编辑:在实验实例下,工作正常,初始化时间约为5秒。然而,一旦部署为VSIX,它就不会触发。
发布于 2010-09-26 01:22:10
我在这里看到了几个问题:
发布于 2012-03-16 17:37:01
尝试执行以下命令:
dte = Package.GetGlobalService(typeof(DTE)) as DTE2;发布于 2010-09-25 13:15:58
如果您有一个MEF组件,那么访问DTE对象的最简单方法如下所示
首先添加一个对Microsoft.VisualStudio.Shell.Immutable.10的引用。然后为SVsServiceProvider添加一个MEF导入。此对象有一个可以查询DTE的GetService方法
[ImportingConstructor]
public MyComponent(SVsServiceProvider serviceProvider) {
_DTE dte = (_DTE)serviceProvider.GetService(typeof(_DTE));
}https://stackoverflow.com/questions/3792333
复制相似问题