waitingforthestorm
waitingforthestorm

Reputation: 95

Get DTE within Tool Window Constructor

I've built a VSPackage with a tool window and am encountering a problem. If the user opens the tool window when VS is already running, I hit the MyToolPackage class and am able to get DTE using

(DTE)GetService(typeof(DTE));

When the user closes VS and starts it again, the tool window is already open and I don't hit MyToolPackage but instead the MyToolWindow class from where GetService always returns null.

Is there any way to get the DTE object from within the MyToolWindow class or another class of my choice?

Thanks in advance.

Upvotes: 1

Views: 231

Answers (2)

Ed Dore
Ed Dore

Reputation: 2119

Better yet, don't attempt to retrieve any services from your toolwindow ctor. The window hasn't been sited yet, so it has no way of retrieving and querying for various services.

You could try using Package.GetGlobalService, but the correct way, would be to place the requisite code into an override of your ToolWindowPane.OnToolWindowCreated. This function is invoked after the toolwindow has been properly sited.

Sincerely,

Upvotes: 1

Related Questions