VS代码片段

VS代码片段官方说明:

https://docs.microsoft.com/zh-cn/visualstudio/ide/visual-csharp-code-snippets?view=vs-2019


可以通过箭头地址将常用的代码搞成自定义的代码片段放到vs里面,可以提高部分编码速度。

用法:以下xml节点放片段快捷键,然后按两次Tab代码就快速生成。

WPF

  • 注册依赖属性代码片段:propdpa.snippet
<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propdpa</Title>
      <Author>Microsoft Corporation</Author>
      <Description>注册附加的依赖属性.</Description>
      <Shortcut>propdpa</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>propertyType</ID>
          <ToolTip>替换为属性的类型.</ToolTip>
          <Default>int</Default>
        </Literal>
        <Literal>
          <ID>propertyName</ID>
          <ToolTip>替换为属性的名称.</ToolTip>
          <Default>MyProperty</Default>
        </Literal>
        <Literal>
          <ID>defaultValue</ID>
          <ToolTip>替换为属性的默认值.</ToolTip>
          <Default>0</Default>
        </Literal>
         <Object>
          <ID>ownertype</ID>
          <Type>Control</Type>
          <ToolTip>替换为要添加此代码段的类的名称.</ToolTip>
          <Default>ownerclass</Default>
        </Object>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
          /// <summary>
          /// 
          /// </summary>
          public static readonly DependencyProperty $propertyName$Property = DependencyProperty.RegisterAttached("$propertyName$", typeof($propertyType$), typeof($ownertype$), new PropertyMetadata($defaultValue$));

          /// <summary>
          /// 
          /// </summary>
          /// <param name="element"></param>
          /// <param name="value"></param>
          public static void Set$propertyName$(DependencyObject element, $propertyType$ value)
          {
              element.SetValue($propertyName$Property, value);
          }

          /// <summary>
          /// 
          /// </summary>
          /// <param name="element"></param>
          /// <returns></returns>
          public static $propertyType$ Get$propertyName$(DependencyObject element)
          {
              return ($propertyType$)element.GetValue($propertyName$Property);
          }
          $end$]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
原文地址:https://www.cnblogs.com/wgx0428/p/13617661.html