VSIX:获取DTE对象 [英] VSIX: Getting DTE object

查看:539
本文介绍了VSIX:获取DTE对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Visual Studio包需要使用EnvDTE.DTE变量,但它总是回来为空。许多黑客阅读了之后,所有的人说,使用OnShellPropertyChange()方法(IVsShellPropertyEvents),但有时它只是永远不会触发 - 好像我从来没有延长完成加载

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.

我使用VS2010和检查对阵双方VSSPROPID_Zombie和ShellInitialized - 没有工作。 :(

I'm using VS2010 and checking against both VSSPROPID_Zombie and ShellInitialized - no work. :(

任何想法?这是code我使用:

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;
        }

编辑:在实验实例,它的作品完美,大约需要5秒钟进行初始化。然而,一旦部署为VSIX - 它根本不火

Under Experimental Instance, it works perfectly and takes about 5 seconds to initialize. However, once deployed as VSIX - it simply doesn't fire.

推荐答案

我看到一对夫妇的问题在这里:

I see a couple of problems here:

  • 您真的应该使用__VSSPROPID4.VSSPROPID_ShellInitialized(在Microsoft.VisualStudio.Shell.Interop.10.0定义),而不是-9083以便于阅读
  • 您应该检查ShellInitialized被设置为真的(虽然检查僵尸去假是正确的)
  • 请记住,ShellInitialized将变成真正的一旦 ...在VS.启动检查它,如果你的包被注册到自动加载启动时(这可能发生之前,VS是完全准备好了),正确的方法。然而,大多数包应该的不可以在启动时自动加载,而是按需从某个用户操作需要你的包code加载。然后,您可以检查DTE业务在你的包中的类初始化方法。
  • You really should be using __VSSPROPID4.VSSPROPID_ShellInitialized (defined in Microsoft.VisualStudio.Shell.Interop.10.0) instead of -9083 for readability
  • You should be checking for ShellInitialized being set to true (although checking for Zombie going to false is correct)
  • Keep in mind that ShellInitialized will change to true once...on startup of VS. Checking for it is the right approach if your package is registered to auto-load on startup (which may happen before VS is fully ready to go). However, most packages should not auto-load on startup, but rather load on-demand from some user action requiring your package code. You can then check for the DTE service in your package class Initialize method.

这篇关于VSIX:获取DTE对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆