VSIX:获取 DTE 对象
我的 Visual Studio 包需要使用 EnvDTE.DTE 变量,但它总是返回为 null。在阅读了许多黑客之后,他们都说要使用 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 - 它根本不会触发。
My Visual Studio package requires the use of an EnvDTE.DTE variable, but it always gets back as null. After reading up on many hacks, all of them say to use the OnShellPropertyChange() method (IVsShellPropertyEvents), but sometimes it just never fires - as if my extension never finishes loading.
I'm using VS2010 and checking against both VSSPROPID_Zombie and ShellInitialized - no work. :(
Any ideas? This is the code I'm using:
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;
}
EDIT: Under Experimental Instance, it works perfectly and takes about 5 seconds to initialize. However, once deployed as VSIX - it simply doesn't fire.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试以下命令:
Try the following command:
如果您有 MEF 组件,获取
DTE
对象的最简单方法如下:首先添加对 Microsoft.VisualStudio.Shell.Immutable.10 的引用。然后为
SVsServiceProvider
添加 MEF 导入。该对象有一个 GetService 方法,可以查询DTE
If you have a MEF component the easiest way to get to a
DTE
object is as followsFirst add a reference to Microsoft.VisualStudio.Shell.Immutable.10. Then add a MEF import for
SVsServiceProvider
. This object has a GetService method which can be queried forDTE
我在这里看到几个问题:
I see a couple of problems here:
我知道您已经选择了一个答案,但您确实指定了您不想使用 MEF,所以我想我会为了完整性而发布一个替代方案....;p
I know you selected an answer already but you did specify that you didnt want to use the MEF so I thought I would post an alternative just for the sake of completeness....;p