代码之家  ›  专栏  ›  技术社区  ›  ilivewithian

我能在vs 2010中有一个代码片段吗?替换的代码片段替换了多个项目

  •  0
  • ilivewithian  · 技术社区  · 14 年前

    我有一个非常简单的代码snitppet,看起来像这样:

    <asp:Label Text="$Name$" runat="server" AssociatedControlID="$txtName$" />
    <asp:TextBox runat="server" ID="$txtName$" />
    <br />
    

    我希望 $txtName$ 在这两个地方都会被更改,但是没有,只有第一个实例被更改,我甚至无法切换到第二个实例。

    这方面有工作要做吗?

    3 回复  |  直到 14 年前
        1
  •  1
  •   Richard    14 年前

    这对我来说是可行的。

    你能展示完整的代码段文件吗?这是一个有效的方法, type fieldName 多次更换:

    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
        <Header>
            <Title>Non-automatically implemented property</Title>
            <Author>Richard Cox</Author>
            <Shortcut>propf</Shortcut>
            <Description>Property with exlicit field</Description>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>propName</ID>
                    <ToolTip>Property Name</ToolTip>
                    <Default>Name</Default>
                </Literal>
                <Literal>
                    <ID>fieldName</ID>
                    <ToolTip>Field Name</ToolTip>
                    <Default>field</Default>
                </Literal>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>string</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[private $type$ $fieldName$;
        public $type$ $propName$ {
            get { return $fieldName$;}
            set { $fieldName$ = value;}
        }
    
        $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
    
        2
  •  1
  •   ilivewithian    14 年前

    结果发现替换程序正在工作,但并不像我键入的那样。所以我可以输入我需要的,然后为了改变额外的属性,我必须将焦点从片段中移开,所以按下向下箭头键就可以完成这项工作。

        3
  •  1
  •   wałdis iljuczonok    14 年前

    有趣的场景开始于您想用$selected$创建两次环绕片段的时候。例如,创建checkArgumentFornull代码段以检查参数是否为空,如果为空,则使用参数名称引发ArgumentNullException。后来才换了。

    有什么想法吗?当然,我们可以自己使用扩展代码段和类型参数名。

    <?xml version="1.0" encoding="utf-8"?>
    <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
      <CodeSnippet Format="1.0.0">
        <Header>
          <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
          </SnippetTypes>
          <Title>notnull</Title>
          <Author>Viiar Consulting</Author>
          <Description>Code snippet for checking whether is argument null</Description>
          <HelpUrl>
          </HelpUrl>
          <Shortcut>notnull</Shortcut>
        </Header>
        <Snippet>
          <Imports>
            <Import>
              <Namespace>System</Namespace>
            </Import>
          </Imports>
          <Declarations>
            <Literal Editable="true">
              <ID>argument</ID>
              <ToolTip>
              </ToolTip>
              <Default>argument</Default>
              <Function>
              </Function>
            </Literal>
          </Declarations>
          <Code Language="csharp"><![CDATA[if ($selected$ == null)
    {
        throw new ArgumentNullException("$selected$");
    }
    $end$]]></Code>
        </Snippet>
      </CodeSnippet>
    </CodeSnippets>