You may have noticed that this blog has been down for several days. Mine was not the only one, all the blogs of Microsoft MVPs at msmvps.com were affected by a severe performance issue of the old Community Server software and now have been migrated to WordPress.

Although creating a toolwindow and hosting a usercontrol in a Visual Studio package seems easy (thanks to the package wizard), initializing the usercontrol to receive an instance of your package (or the value of some property of your package) is somewhat tricky.

I have seen this question from time to time in the forums and until now my (wrong) answer was to use the ShowToolWindow event handler method of the package to get the instance of the usercontrol from the ToolWindowPane (using its Content property for WPF usercontrols and its Window property for Windows Forms usercontrols). Once you have the usercontrol instance, you can call a method of the usercontrol to pass it any piece of information that you want to initialize it. I won’t show the code because it is an incorrect approach due to this: when using a toolwindow in a package, if the last time that you closed Visual Studio the toolwindow was visible, the next time that you open Visual Studio the toolwindow will be shown automatically without the ShowToolWindow event handler method being called (so your usercontrol wouldn’t be initialized in that scenario).

This didn’t happen with add-ins (toolwindows were not open automatically in the next session if left open in the previous session). But with packages it happens, so the ShowToolWindow method of the package is not the proper place to initialize the usercontrol. Then, it must be done in the MyToolWindow class, which creates the usercontrol and it is always called, and fortunately it has a Package base property that you can use to get the instance of your package. Alas, that instance is null in the toolwindow constructor. My latest article shows a workaround:

When you create a toolwindow with the Visual Studio package wizard, a Windows Presentation Foundation (WPF) usercontrol is used. If you are migrating an add-in to a package, chances are that your toolwindows use Windows Form usercontrols and you don’t want to change the forms technology (migrating the add-in to a package is enough work…). It happens that the way to host a usercontrol in a toolwindow is different for WPF and Windows Forms. In the former case you only have to assign the Content property to the instance of the usercontrol. If you do the same in the latter case, nothing happens (you don’t even get an exception). For Windows Forms usercontrols, you need to override the Window property to return the instance of the usercontrol.

I am using Visual Studio Online since a couple of months ago to great satisfaction adopting more and more of its features. One of them is continuous integration (CI) and gated check-ins. Yesterday I tried to check-in a modification of one of my add-ins converted to a Visual Studio package for the first time and the build failed with this error:

C:\Program Files
(x86)\MSBuild\Microsoft\VisualStudio\<version>\VSSDK\Microsoft.VsSDK.Common.targets
(line): Can not find the installation for VS SDK.

Using the automation model (EnvDTE) you would use DTE.Solution to get the solution object and then the Solution.IsOpen, Solution.Name, Solution.FullName or Solution.Properties.Item(“Path”).Value properties and so on.

In this small article I explain how to use the IVsSolution interface of the SVsSolution service to avoid the use of EnvDTE.Solution to get some information about the loaded solution:

Two of the three strategies that I explained in my last post Strategies migrating from Visual Studio add-ins to packages involve to avoid the use of the automation model (EnvDTE). When you use an EnvDTE object, basically you do one of three things: to get/set properties, to invoke methods to perform actions and to get events. So, you need to learn how to do that with Visual Studio interfaces of services.

If you follow my third strategy, likely you will define an interface named IHost with some properties like this:

When implementing the RegistryRoot property of that interface using EnvDTE, you would use the DTE.RegistryRoot property. And to implement the InstallationFolder property, you would read the value “ProductDir” of the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\<version>\Setup\VS.

When implementing those properties of the interface in your package, in this small article I explain how to use the IVsShell interface of the SVsShell service to avoid the use of EnvDTE.DTE to get some information about the Visual Studio IDE (the shell).