如何在 Visual Studio Snippet 中重复变量

问题描述 投票:0回答:1

我想为这个片段输入一次属性名称,并让它填写所有三个占位符。当我目前在 Visual Studio 中尝试这个时,它只填充第一个变量。有没有办法通过一次输入名称来填写所有三个?

  <div class="input-container clearfix">
            <div class="input-row clearfix">
                <div class="bp2-wdn-col-one-fourth input-title">
                    @Html.LabelFor(model => model.$PropertyName$, htmlAttributes: new { @class = "" })
                </div>
                <div class="bp2-wdn-col-three-fourths">
                    @Html.EditorFor(model => model.$PropertyName$, new { htmlAttributes = new { @class = "" } })
                    @Html.ValidationMessageFor(model => model.$PropertyName$, "", new { @class = "" })
                </div>
            </div>
        </div>

并且该属性在上面定义为

  <Declarations>
  <Literal>
    <ID>PropertyName</ID>
    <ToolTip>Name of the model property</ToolTip>
    <Default>Property</Default>
  </Literal>
</Declarations>

有没有办法通过一次输入名称来填写所有三个?

visual-studio visual-studio-2015
1个回答
0
投票

我知道这是旧的,但以防万一有人从搜索引擎登陆这里,这里的关键(hack?)是将

Default
设置为与您的
ID
相同。下面是一个基于 Windows Community Toolkit 生成异步中继命令的示例。在 VS2022 中为我工作:

<?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>
      </SnippetTypes>
      <Title>Async Command based on Windows Community Toolkit</Title>
      <Author>
      </Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>wcmda</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>Name</ID>
          <ToolTip>Name of the command</ToolTip>
          <Default>Name</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>Method</ID>
          <ToolTip>The method that is to be called asynchronously</ToolTip>
          <Default>Method</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Delimiter="$"><![CDATA[#region $Name$AsyncCommand
private AsyncRelayCommand _$Name$AsyncCommand;

/// <summary>
/// 
/// </summary>
public AsyncRelayCommand $Name$AsyncCommand => _$Name$AsyncCommand ??= new AsyncRelayCommand(async () =>
{
    await $Method$
},
() => true);
#endregion]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

将它保存到您的代码片段文件夹中(通常在您的

Documents\Visual Studio 2022\Code Snippets\Visual C#\My Code Snippets
文件夹中),然后启动VS2022并在CS文件中键入
wcmda
并按两次Tab。它将生成命令并选择第一次出现的
Name
。键入您的命令名称,然后再次按 Tab。这将替换命令中所有出现的
Name
,并带您到
Method
.

© www.soinside.com 2019 - 2024. All rights reserved.