Create a WPF based ToolWindow in your Add-In
One of the big changes in Visual Studio 2010 is the introduction of the WPF based UI which means that (amongst other things) you can now host WPF windows directly within the IDE without having to wrap them inside WinForm controls.
Unfortunately you may notice that if you create a simple WPF UserControl and then try to create a ToolWindow from it using CreateToolWindow2, you’ll run into the following Managed Debugging Assistant:
Managed Debugging Assistant 'NonComVisibleBaseClass' has detected a problem in 'c:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe'.
Additional Information: A QueryInterface call was made requesting the default IDispatch interface of COM visible managed class 'WPFUserControlAddIn.WPFToolWindow'.
However since this class does not have an explicit default interface and derives from non COM visible class 'System.Windows.Controls.UserControl', the QueryInterface call will fail.
This is done to prevent the non COM visible base class from being constrained by the COM versioning rules.
If you follow through this error message it will lead you to the MSDN page for this error (here) and from there to the page that holds the solution.
The Solution
Add the following to your [yourfile].xaml.cs around your class declaration:
using System.Runtime.InteropServices;
using stdole;
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
class [yourclass] : UserControl, IDispatch
Note! Don’t forget the addition of the IDispatch implementation (there’s nothing to implement, don’t worry). Without it you’ll get "InvalidCastException" from mscordb.lib and a null in your ref object (the last parameter to CreateToolWindow2.
Got a Sample For Us?
You bet – grab the sample solution. All you need to do is extract, add the .addin file to you AddIns directory (under Documents\Visual Studio 2010\Addins) and away you go.
Sample solution url is broken!
It worked, thanks a lot!
It works but when I dock the tool window. the title is empty unlike Windows Forms tool window. any ideas how to fix this ???
I’m not sure, I haven’t used the dock – though none of this process should be required in the newest VS and it should “just work”…