Adding Visual Studio Commands to a Start Page

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

When you create a custom start page, you can add Visual Studio commands to it. This document discusses the different ways to bind Visual Studio commands to XAML objects on a start page.

For more information about commands in XAML, see Commanding Overview

The start page created in Creating a Custom Start Page added the Microsoft.VisualStudio.PlatformUI and Microsoft.VisualStudio.Shell namespaces, as follows.

xmlns:vs="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.14.0"  
xmlns:vsfx="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.14.0"  

Add another namespace for Microsoft.VisualStudio.Shell from the assembly Microsoft.VisualStudio.Shell.Immutable.11.0.dll. (You may need to add a reference to this assembly in your project.)

xmlns:vscom="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.Immutable.11.0"  

You can use the vscom: alias to bind Visual Studio commands to XAML controls on the page by setting the Command property of the control to vscom:VSCommands.ExecuteCommand. You can then set the CommandParameter property to the name of the command to execute, as shown in the following example.

<Button Name="btnNewProj" Content="New Project"   
    Command="{x:Static vscom:VSCommands.ExecuteCommand}"  
    CommandParameter="File.NewProject" >  
</Button>  

System_CAPS_ICON_note.jpg Note

The x: alias, which refers to the XAML schema, is required at the beginning of all commands.

You can set the value of the Command property to any command that can be accessed from the Command window. For a list of available commands, see Visual Studio Command Aliases.

If the command to add requires an additional parameter, you can add it to the value of the CommandParameter property. Separate parameters from commands by using spaces, as shown in the following example.

<Button Content="Web Search"   
        Command="{x:Static vscom:VSCommands.ExecuteCommand}"  
        CommandParameter="View.WebBrowser www.bing.com" />  

Calling Extensions from the Command Well

You can call commands from registered VSPackages by using the same syntax that is used to call other Visual Studio commands. For example, if an installed VSPackage adds a Home Page command to the View menu, you can call that command by setting CommandParameter to View.HomePage.

System_CAPS_ICON_note.jpg Note

If you call a command that is associated with a VSPackage, the package must be loaded when the command is invoked.

To call a command from an assembly, or to access code in a VSPackage that is not associated with a menu command, you must create an alias for the assembly and then call the alias.

To call a command from an assembly

  1. In your solution, add a reference to the assembly.

  2. At the top of the StartPage.xaml file, add a namespace directive for the assembly, as shown in the following example.

    xmlns:vsc="clr-namespace:WebUserControl;assembly=WebUserControl"  
    
    
  3. Invoke the command by setting the Command property of a XAML object, as shown in the following example.

    Xaml

    <vs:Button Text="Hide me" Command="{x:Static vsc:HideControl}" .../>  
    
    
System_CAPS_ICON_note.jpg Note

You must copy your assembly and then paste it in ..\Visual Studio installation folder\Common7\IDE\PrivateAssemblies\ to make sure it is loaded before it is called.

You can access the DTE object from a Start Page, both in markup and in code.

In markup, you can access it by using the Binding Markup Extension syntax to call the DTE object. You can use this approach to bind to simple properties such as those that return collections, but you cannot bind to methods or services. The following example shows a TextBlock control that binds to the Name property, and a ListBox control that enumerates the Caption properties of the collection that is returned by the Windows property.

<TextBlock Text="{Binding Path=DTE.Name}" FontSize="12" HorizontalAlignment="Center"/>  
<ListBox ItemsSource="{Binding Path=DTE.Windows}">  
    <ListBox.ItemTemplate>  
        <DataTemplate>  
            <TextBlock Text="{Binding Path=Caption}"/>  
        </DataTemplate>  
    </ListBox.ItemTemplate>  
</ListBox  

For an example, see Walkthrough: Saving User Settings on a Start Page.

Adding User Control to the Start Page

Show: