简体   繁体   English

具有lambda格式的Visual Studio自动完成事件处理程序

[英]Visual Studio autocomplete event handler with lambda format

I'm on VS2012 and want to use the lambda format for event handling, however VS does autocomplete with the tab key whenever you type an event subscrition via +=, eg: 我在VS2012上并希望使用lambda格式进行事件处理,但是只要通过+ =键入事件子目录,VS就会使用tab键自动完成,例如:

VS autocompleted with a reference to a function an inserts the function: VS自动完成对函数的引用并插入函数:

txtTitle.TextChanged += txtTitle_TextChanged;

void txtTitle_TextChanged(object sender, TextChangedEventArgs e)
{
    ....
}

Is there any way to force autocomplete with Lambda format of: 有没有办法强制使用Lambda格式的自动完成:

txtTitle.TextChanged += (object sender, TextChangedEventArgs e) =>
{
    ....
}

Its a huge pain to have to copy and paste from the autocompleted non-lambda to the tighter lambda format. 从自动完成的非lambda复制和粘贴到更紧凑的lambda格式是一件巨大的痛苦。

You can just create a code snippet, I have one for creating Lambda events. 你可以创建一个代码片段,我有一个用于创建Lambda事件的代码片段。

here is the snippet if you want to try (just save as whatever .snippet) and import in VS (Tools -> Code Snippet Manager) 如果你想尝试(只保存为任何 .snippet)并在VS中导入(工具 - >代码片段管理器),这里是片段

Snippet: 片段:

<?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>SnippetFile1</Title>
      <Author>sa_ddam213</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>le</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>s</ID>
          <ToolTip>s</ToolTip>
          <Default>s</Default>
          <Function>
          </Function>
        </Literal>
        <Literal Editable="true">
          <ID>e</ID>
          <ToolTip>e</ToolTip>
          <Default>e</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="method body"><![CDATA[($s$,$e$) => { };]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Then to use just type the eventname += le Tab 然后使用只需键入eventname + = le Tab

Example

Loaded += le Tab 加载+ = le Tab

Result 结果

Loaded += (s, e) => { };

你可以写:

this.txtTitle.TextChanged += (s, e) => {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM