Creating Visual Studio Package for tool windows and editor menus

Sometimes when the requirements are too high and not related to the editor itself, there is also an option to create a Visual Studio Package for the project. The Visual Studio Package project creates a basic VSPackage. The template has the written code ready to implement a MenuItem class or a tool window.

Note

As we already know that the editor is built using WPF, we also need to know the basics of WPF to implement the tool window inside the IDE.

Getting ready

Open Visual Studio and create a project from Visual Studio Package. Visual Studio Package is a complete installation of Visual Studio, which is signed with a valid key. We name the project VSPackageSample.

Once you select the project type, you will be provided with a wizard, as shown in the following screenshot:

Getting ready

As we know, the Visual Studio Package is a complete install for Visual Studio, so it needs a singing assembly. We choose Generate a new key file to sign the assembly to move forward. You can even select a key for yourself using the Use the following key file option, as shown in the following screenshot:

Getting ready

The next screenshot asks for the company name and basic package configuration. You can pick the icon here for the project and also provide a detailed description.

Here, we also need to choose what type of extensions we want to include.

Getting ready

There are three options. One is Menu Command, which will add a menu to the tools menu and can be invoked to call a method you specify. You can also choose Tool Window to open a window and Custom Editor to edit something inside the IDE. The tool window is a WPF control, while the custom editor is Windows Forms Control.

How to do it...

Now, let's create a VSPackage extension to implement such an editor:

  1. After you finish the wizard, the entire project is created. There are a number of files to work on. The very entry point of the project is the VSPackageSamplePackage.cs file. Open the file and you will see a class that inherits from the Package class.
  2. In the Package Members section, you will see how the menu and the command are initialized:
    // Add our command handlers for menu (commands must exist in the .vsct file)
    OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
    if ( null != mcs )
    {
        // Create the command for the menu item.
        CommandID menuCommandID = new CommandID(GuidList.guidVSPackageSampleCmdSet, (int)PkgCmdIDList.cmdidMyCommand);
        MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );
        mcs.AddCommand( menuItem );
        // Create the command for the tool window
        CommandID toolwndCommandID = new CommandID(GuidList.guidVSPackageSampleCmdSet, (int)PkgCmdIDList.cmdidMyTool);
        MenuCommand menuToolWin = new MenuCommand(ShowToolWindow, toolwndCommandID);
        mcs.AddCommand( menuToolWin );
    }

    In the preceding code, a MenuCommand class is created for menuItem and ToolWindow. Both are added to the OleMenuCommandService class. The MenuCommandService class lets you define the menu for the commands.

  3. When the menu is clicked, ShellMessage is invoked to show MessageBox on the IDE. The menu will appear in the Tools menu. The MenuItemCallback class uses uiShell.ShowMessageBox to show the message:
    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(
                           0,
                           ref clsid,
                           "VSPackageSample",
                           string.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", this.ToString()),
                           string.Empty,
                           0,
                           OLEMSGBUTTON.OLEMSGBUTTON_OK,
                           OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
                           OLEMSGICON.OLEMSGICON_INFO,
                           0,        // false
                           out result));

    You can write your custom code to invoke selection of the menu.

  4. On the contrary, ToolWindow opens up the MyControl.xaml file. You can design ToolWindow using XAML and show the content directly inside Visual Studio:
    ToolWindowPane window = this.FindToolWindow(typeof(MyToolWindow), 0, true);
                if ((null == window) || (null == window.Frame))
                {
                    throw new NotSupportedException(Resources.CanNotCreateWindow);
                }
                IVsWindowFrame windowFrame = (IVsWindowFrame)window.Frame;
                Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(windowFrame.Show());

    The window frame is created and the control gets loaded inside the frame to ensure there is a consistent feel to the IDE.

  5. Open the MyControl.xaml file and you will see a button placed inside the IDE, so that when the control is invoked, MessageBox will be shown. You can open ToolWindow by navigating to View | OtherWindow | CommandExplorer.
    How to do it...

    The window currently does nothing. It has a button, which can be clicked to open a MessageBox.

  6. There are some additional files such as EditorFactory. This is the main editor class that handles the instance creation and destruction of the editor. The EditorFactory class is registered to the extension and automatically called whenever any component of the editor is called.
  7. Finally, when your package is built successfully, you can double-click on the vsix file on the debug screen to run and install the extension to Visual Studio.

How it works...

The VS Package is the main packager for Visual Studio extensions. Visual Studio packager allows you to add UI elements to the IDE to provide a better experience for the user. You can deploy any package, either distributing manually or through the Visual Studio gallery, such that developers can download and use the extension that you have created.

The main criterion for the package to execute is written inside the VSPackageSamplePackage file. Here, the ProvideToolWindow function registers the extension tool window for the package. The EditorFactory class is registered for additional extension components and the VSPackageSamplePackage class itself creates the menu items.

After you compile the project, a VSIX file gets created. This file is nothing but a zipped archive with all the deployable components. Rename the file to a .zip extension, and you will see the following content:

How it works...

The pkgdef file is the package definition file, which stores all the metadata associated with the extension. The extension.manifest file is the manifest file for the extension. The manifest file includes the name of the extension, details, description, and so on. The main code is compiled as a .dll file, which represents the actual extension.

The Visual Studio extensions can be deployed and/or uploaded directly to the Visual Studio gallery at http://visualstudiogallery.msdn.microsoft.com/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.158.32