Share via


Adding Visual Studio Commands to a Start Page

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

Adding Commands from the Command Well

The Start Page project template defines aliases for the Microsoft.VisualStudio.PlatformUI and Microsoft.VisualStudio.Shell namespaces, as follows.

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

You can use the vs: alias to bind Visual Studio commands to XAML controls on the page by setting the Command property of the control to vs: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 vs:VSCommands.ExecuteCommand}"
        CommandParameter="File.NewProject" >

</Button>
        

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 Predefined 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 vs: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.

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 that is associated with a Visual Studio add-in, you must use the full command name of the add-in. For more information, see How to: Run Add-Ins on the Command Line.

Adding Commands from Assemblies

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}" .../>
    

Note

If you are not using the Start Page project template, 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.

Adding Commands from the Automation Model

You can access the Visual Studio automation model from a Start Page, both in markup and in code.

In markup, you can access the Automation model by using the Binding Markup Extension syntax to call the DTE object. By using this approach, you can 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>

In code, you can access the methods, services, and more complex properties of the Automation model, for example, in a user control or in the code-behind file for your XAML page. Do this the same manner as in any other project that uses the Automation model. For an example, see Walkthrough: Saving User Settings on a Start Page By Using Automation.

For more information about the Automation model, see Extending the Visual Studio Environment or Referencing Automation Assemblies and the DTE2 Object.k

Relative Paths

When you reference file paths from a Start Page, always use a relative path to allow for different system configurations. However, the root of all relative paths on a Start Page resolves not to the \StartPages\ folder but to ..\Visual Studio installation folder\Common7\IDE\, which is where devenv.exe is located. To set a path relative to the location of the Start Page files, use the VSStartPageRelativeConverter. Do this by setting the Source property of the object to vs:StartPageRelative, as shown in the following example.

<Image Source="{sp:StartPageRelative Image1.png}" Margin="20" /> 

Use standard relative path syntax when you access resources that are included in Visual Studio, or files that are included in other packages.

See Also

Tasks

Walkthrough: Adding a DLL Reference to the Start Page

Concepts

Start Page Architecture

Start Page Best Practices

Other Resources

Custom Start Pages