Visual Studio: Is there XAML snippets support?
Asked Answered
S

3

8

Is there any way to add XAML snippets for XAML editor in Visual Studio 2010? For example I'd like to save my self from some typing and have something like this:

snippet: gr<TAB>

generates code:

<Grid>
    <Grid.RowDefinitions>

    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>

    </Grid.ColumnDefinitions>
</Grid>

Then if I type for example: rd<TAB> it should generate <RowDefinition />

Similarly, cd<TAB> should generate <ColumnDefinition />

and similarly for other XAML elements.

I think you get the idea... The point is to make these snippets available only in XAML editor of the Visual Studio. Having them in other code editors inside Visual Studio (eg. when editing .cs files) doesn't have point.

Silky answered 26/4, 2012 at 20:41 Comment(1)
This afternoon I created a few XAML snippets (for Grid.ColumnDefinitions) using JetBrains Resharper. Obviously this is not an answer to your question as I don't know if you have a Resharper license.Hyperventilation
V
2

There is no built in support, but there is an extension to provide something similar to this.

Voyeur answered 26/4, 2012 at 20:43 Comment(0)
M
6

It works in VS 2019. The guide to use: Creating Code Snippets

Example, to insert:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <Tag Grid.Row="0" Grid.Column="0" />
</Grid>

Tag being an editable value to be set each time you insert the snippet, use this code:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <SnippetTypes>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
    <Title>Grid Default</Title>
    <Description>Grid With Default Rows and Columns</Description>
    <HelpUrl>
    </HelpUrl>
    <Shortcut>griddef</Shortcut>
  </Header>
  <Snippet>
    <Declarations>
      <Literal Editable="true">
        <ID>TagName</ID>
        <ToolTip>Tag Name</ToolTip>
        <Default>TextBlock</Default>
      </Literal>
    </Declarations>

    <Code Language="xaml">
      <![CDATA[
      <Grid>
          <Grid.RowDefinitions>
              <RowDefinition Height="Auto"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto"/>
          </Grid.ColumnDefinitions>

          <$TagName$ Grid.Row="0" Grid.Column="0" />
      </Grid>
      $selected$$end$]]>
    </Code>
  </Snippet>
</CodeSnippet>

This file can be saved anywhere with the extension .snippet, then use the snippets manager (Tools | Code Snippets Manager... to import the file to the standard place used by IntelliSense (don't forget to select type XAML). You may also create the file directly at the final place (visible in the snippet manager dialog below) to save the import step.

enter image description here

The shorcut may not appear in IntelliSense (this was my case, the reason is still unknown to me), but typing griddef TabTab will insert the template.

For your specific needs, replace the code in CDATA element, and change the value in Shortcut element. You may also remove this section:

<Declarations>
  <Literal Editable="true">
    <ID>TagName</ID>
    <ToolTip>Tag Name</ToolTip>
    <Default>TextBlock</Default>
  </Literal>
</Declarations>

if you don't need any editable value, or adapt it according to the guide mentioned at the beginning.

Mackler answered 26/7, 2019 at 10:45 Comment(0)
S
3

If you don't need parameters, then you can use the Toolbox!

Simply drag the code into the Toolbox and insert it wherever you want. This works for C# as well as for XAML.

From: http://blog.wpfwonderland.com/2012/04/16/xaml-snippets-and-visual-studio/

Suazo answered 12/4, 2013 at 7:23 Comment(1)
The wpfwonderland.com host server crashed and the XAML snippets article is gone.Squarerigger
V
2

There is no built in support, but there is an extension to provide something similar to this.

Voyeur answered 26/4, 2012 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.