VS代码片段(CodeSnippet)的制作以及常用代码片段记录

总目录



前言

本文将会探索VS中代码片段,并且学会自己制作代码片段,通过这种方式提高我们的编程效率。


一、代码片段是什么?

1.了解

先看下下面这段代码的编写
在这里插入图片描述
是不是很熟悉?平常我们编写代码的使用,经常会使用prop,ctor等快速生成代码。
那么它到底是啥呢?我们能不能自己也写一个这些的快捷生成代码的方式呢?答案是:当然可以!

2.查找代码片段文件夹

VS 中 选择【工具】,然后选择【代码片段管理器】,然后语言选择【CSharp】,如下图所示:
在这里插入图片描述
我们可以看到目录下有我们平常使用的一些快捷方式,现在我们进入上面【位置】的目录中:

C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC#\Snippets\2052\Visual C#

在这里插入图片描述
该文件夹下,就是存放代码片段的文件夹,那么我们后续自己编写的代码片段,放在这个文件夹下就可以使用了。

二、编写代码片段

1.认识代码片段

打开一个常用的for循环的snippet文件看看:
在这里插入图片描述
一个xml文档,CodeSnippet 里面主要有Header 和 Snippet。

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<!--Header 主要是对该快捷方式的描述-->
		<Header>
			<Title>for</Title><!--标题-->
			<Shortcut>for</Shortcut><!--这里就是定义快捷方式-->
			<Description>for 循环的代码片段</Description><!--描述文字-->
			<Author>Microsoft Corporation</Author><!--作者-->
			<SnippetTypes><!--类型-->
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			<SnippetType>Refactoring</SnippetType>
			<!--SnippetType 有三种:Expansion 和SurroundsWith 和Refactoring
				常用:Expansion 和SurroundsWith 
			    Expansion:允许将代码段插入到光标处。
				
				SurroundsWith:允许将此代码段放置在一段选定的代码周围。
				比如我们写完一段代码后,发现忘记加 try...catch... 了,
				这个时候可以选中需要包裹在 try...catch... 中的代码,然后调用 Code Snippet。				
			-->
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations><!--申明变量,供Code节点使用-->
				<Literal>
					<ID>index</ID>
					<Default>i</Default>
					<ToolTip>索引</ToolTip>
				</Literal>
				<Literal>
					<ID>max</ID>
					<Default>length</Default>
					<ToolTip>最大长度</ToolTip>
				</Literal>
			</Declarations>
			<Code Language="csharp"><!--核心部分:具体代码片段展示形式-->
				<![CDATA[for (int $index$ = 0; $index$ < $max$; $index$++)
			{
			$selected$ $end$
			}]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

2.编写自定义代码片段

1、trycf (try,catch,finally)

平常使用异常捕获的时候只有try 和 tryf 但是没有try,catch,finally完整形式的捕获,我们自定义一个如下所示:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>trycf</Title>
			<Shortcut>trycf</Shortcut>
			<Description>try catch finally 的代码片段</Description>
			<Author>lzk</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>expression</ID>
					<ToolTip>异常类型</ToolTip>
					<Function>SimpleTypeName(global::System.Exception)</Function>
				</Literal>
			</Declarations>
			<Code Language="csharp">
			<![CDATA[try 
	    {	        
		    $selected$
	    }
		catch ($expression$)
		{
			throw;
		}
	    finally
	    {
		    $end$
	    }]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

2、propp(用于MVVMLight中)

平常在我们使用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 方法代码更为简洁,具体代码片段如下:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>propp</Title>
			<Shortcut>propp</Shortcut>
			<Description>MVVMLight中具有通知功能的属性代码片段</Description>
			<Author>lzk</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>type</ID>
					<ToolTip>属性类型</ToolTip>
					<Default>int</Default>
				</Literal>
				<Literal>
					<ID>property</ID>
					<ToolTip>属性名</ToolTip>
					<Default>MyProperty</Default>
				</Literal>
				<Literal>
					<ID>field</ID>
					<ToolTip>支持此属性的变量</ToolTip>
					<Default>myVar</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[private $type$ $field$;

	public $type$ $property$
	{
		get { return $field$;}
		set { Set(ref $field$ , value);}
	}
	$end$]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

3、ts (创建一个test方法)

平常有时候测试需要写一个临时的测试方法,如下图所示:

private void Test()
{ 
        
}

代码片段如下:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
	<CodeSnippet Format="1.0.0">
		<Header>
			<Title>ts</Title>
			<Shortcut>ts</Shortcut>
			<Description>测试方法的代码片段</Description>
			<Author>lzk</Author>
			<SnippetTypes>
				<SnippetType>Expansion</SnippetType>
				<SnippetType>SurroundsWith</SnippetType>
			</SnippetTypes>
		</Header>
		<Snippet>
			<Declarations>
				<Literal>
					<ID>name</ID>
					<ToolTip>测试方法名</ToolTip>
					<Default>Test</Default>
				</Literal>
			</Declarations>
			<Code Language="csharp"><![CDATA[private void $name$ ()
	{
		$selected$$end$
	}]]>
			</Code>
		</Snippet>
	</CodeSnippet>
</CodeSnippets>

4、更改propa 和propdp

当我们使用propa和propdp写依赖属性和附加属性的时候,总会生成如下代码:
在这里插入图片描述
但是原生的这两个片段有如上的缺点,因此我们做如下修改:

  • propa
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Define an attached DependencyProperty</Title>
            <Shortcut>propa</Shortcut>
            <Description>Code snippet for an attached property using DependencyProperty as the backing store</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property Type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property Name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
                <Literal>
                    <ID>ownerclass</ID>
                    <ToolTip>The owning class of this Property.  Typically the class that it is declared in.</ToolTip>
                    <Default>ownerclass</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[
                
public static $type$ Get$property$(DependencyObject obj)
{
    return ($type$)obj.GetValue($property$Property);
}

public static void Set$property$(DependencyObject obj, $type$ value)
{
    obj.SetValue($property$Property, value);
}

public static readonly DependencyProperty $property$Property =
    DependencyProperty.RegisterAttached("$property$", typeof($type$), typeof($ownerclass$), new PropertyMetadata(default($type$)));

$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

  • propdp
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Define a DependencyProperty</Title>
            <Shortcut>propdp</Shortcut>
            <Description>Code snippet for a property using DependencyProperty as the backing store</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property Type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property Name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
                <Literal>
                    <ID>ownerclass</ID>
                    <ToolTip>The owning class of this Property.  Typically the class that it is declared in.</ToolTip>
                    <Default>ownerclass</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[

public $type$ $property$
{
    get { return ($type$)GetValue($property$Property); }
    set { SetValue($property$Property, value); }
}

public static readonly DependencyProperty $property$Property = 
    DependencyProperty.Register("$property$", typeof($type$), typeof($ownerclass$), new PropertyMetadata(default($type$)));

$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

3.使用自定义代码片段

  • 找到C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC#\Snippets\2052\Visual C# 文件夹
  • 随便找一个snippet文件复制出来,先将原内容删除,然后将以上内容全覆盖进去,保存
  • 然后在代码片段管理器中点击【导入】按钮,将自定义的代码片段文件导入即可使用。

总结

以上就是今天要介绍的内容,希望以上内容可以帮助到大家,如文中有不对之处,还请批评指正。

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值