本文将会探索VS中代码片段,并且学会自己制作代码片段,通过这种方式提高我们的编程效率。
先看下下面这段代码的编写
是不是很熟悉?平常我们编写代码的使用,经常会使用prop,ctor等快速生成代码。
那么它到底是啥呢?我们能不能自己也写一个这些的快捷生成代码的方式呢?答案是:当然可以!
VS 中 选择【工具】,然后选择【代码片段管理器】,然后语言选择【CSharp】,如下图所示:
我们可以看到目录下有我们平常使用的一些快捷方式,现在我们进入上面【位置】的目录中:
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC#\Snippets\2052\Visual C#
该文件夹下,就是存放代码片段的文件夹,那么我们后续自己编写的代码片段,放在这个文件夹下就可以使用了。
打开一个常用的for循环的snippet文件看看:
一个xml文档,CodeSnippet 里面主要有Header 和 Snippet。
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>forTitle>
<Shortcut>forShortcut>
<Description>for 循环的代码片段Description>
<Author>Microsoft CorporationAuthor>
<SnippetTypes>
<SnippetType>ExpansionSnippetType>
<SnippetType>SurroundsWithSnippetType>
<SnippetType>RefactoringSnippetType>
SnippetTypes>
Header>
<Snippet>
<Declarations>
<Literal>
<ID>indexID>
<Default>iDefault>
<ToolTip>索引ToolTip>
Literal>
<Literal>
<ID>maxID>
<Default>lengthDefault>
<ToolTip>最大长度ToolTip>
Literal>
Declarations>
<Code Language="csharp">
Code>
Snippet>
CodeSnippet>
CodeSnippets>
平常使用异常捕获的时候只有try 和 tryf 但是没有try,catch,finally完整形式的捕获,我们自定义一个如下所示:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>trycfTitle>
<Shortcut>trycfShortcut>
<Description>try catch finally 的代码片段Description>
<Author>lzkAuthor>
<SnippetTypes>
<SnippetType>ExpansionSnippetType>
<SnippetType>SurroundsWithSnippetType>
SnippetTypes>
Header>
<Snippet>
<Declarations>
<Literal>
<ID>expressionID>
<ToolTip>异常类型ToolTip>
<Function>SimpleTypeName(global::System.Exception)Function>
Literal>
Declarations>
<Code Language="csharp">
Code>
Snippet>
CodeSnippet>
CodeSnippets>
平常在我们使用MVVMLight的时候,ViewModelBase中有一个Set方法使我们实现属性通知的时候很方便,我们同样可以将其制作成代码片段。
//使用MVVMLight框架的时候,里面属性通知有两个方法
//【第一种方式】:RaisePropertyChanged方法
private string phone;
public string Phone
{
get { return phone; }
set { phone = value; RaisePropertyChanged(); }
}
//【第二种方式】:Set方法
private string name;
public string Name
{
get { return name; }
set { Set(ref name, value); }
}
显然使用Set 方法代码更为简洁,具体代码片段如下:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>proppTitle>
<Shortcut>proppShortcut>
<Description>MVVMLight中具有通知功能的属性代码片段Description>
<Author>lzkAuthor>
<SnippetTypes>
<SnippetType>ExpansionSnippetType>
SnippetTypes>
Header>
<Snippet>
<Declarations>
<Literal>
<ID>typeID>
<ToolTip>属性类型ToolTip>
<Default>intDefault>
Literal>
<Literal>
<ID>propertyID>
<ToolTip>属性名ToolTip>
<Default>MyPropertyDefault>
Literal>
<Literal>
<ID>fieldID>
<ToolTip>支持此属性的变量ToolTip>
<Default>myVarDefault>
Literal>
Declarations>
<Code Language="csharp">
Code>
Snippet>
CodeSnippet>
CodeSnippets>
平常有时候测试需要写一个临时的测试方法,如下图所示:
private void Test()
{
}
代码片段如下:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>tsTitle>
<Shortcut>tsShortcut>
<Description>测试方法的代码片段Description>
<Author>lzkAuthor>
<SnippetTypes>
<SnippetType>ExpansionSnippetType>
<SnippetType>SurroundsWithSnippetType>
SnippetTypes>
Header>
<Snippet>
<Declarations>
<Literal>
<ID>nameID>
<ToolTip>测试方法名ToolTip>
<Default>TestDefault>
Literal>
Declarations>
<Code Language="csharp">
Code>
Snippet>
CodeSnippet>
CodeSnippets>
C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC#\Snippets\2052\Visual C#
文件夹以上就是今天要介绍的内容,希望以上内容可以帮助到大家,如文中有不对之处,还请批评指正。