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.
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
mscordb.lib and a
null in your
ref object (the last parameter to
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.