Visual Studio 的代码片段(Code Snippet)功能
?
??? 1、概述
???? 在 Visual Studio 中,有一個快捷編輯代碼的功能,比如當我們在 VS 編輯器(C#)中寫出
關鍵字 foreach 后,敲擊一下 Tab 鍵,VS 就幫我們自動補全:(插入代碼段的快捷鍵 Ctrl + K + X)
foreach (var item in collection){}?
???? ?? 我的電腦是? Windows8?的系統,編輯器版本是 Visual Studio 2012,編輯器默認安裝的代碼片段路徑:
???? C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Snippets\2052\Visual C#
???? 同時 VS 也提供了方便開發者自定義代碼片段,文件默認存放路徑:
???? C:\Users\boqiong\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets
????
打開 VS 的“代碼段管理器”: 工具 ---> 代碼段管理器? (快捷鍵:Ctrl + K + B)
可以把自定義的代碼段文件通過點擊“導入”按鈕,添加到自己的代碼段文件夾(My Code Snippets)
? 2、編輯自己的代碼段???????
??? snippet 是xml文件,以.snippet后綴名,下面的自定義代碼段的文件名為:sldpc.snippet
??? 這里自定義一個 Silverlight 中的依賴屬性代碼段:
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"><CodeSnippet Format="1.0.0"><Header><Title>Silverlight DependencyProperty</Title><Shortcut>sldpc</Shortcut><Description>Silverlight 依賴屬性(DependencyProperty)的定義</Description><Author>dbq</Author><SnippetTypes><SnippetType>Expansion</SnippetType></SnippetTypes></Header><Snippet><Declarations><Literal Editable="true"><ID>type</ID><ToolTip>屬性類型</ToolTip><Default>int</Default><Function></Function></Literal><Literal Editable="true"><ID>property</ID><ToolTip>屬性名稱</ToolTip><Default>Name</Default><Function></Function></Literal><Literal Editable="true"><ID>defaultvalue</ID><ToolTip>默認值</ToolTip><Default>0</Default><Function></Function></Literal><Literal Editable="false"><ID>ownerclass</ID><ToolTip>該依賴屬性所屬的類</ToolTip><Default>ClassNamePlaceholder</Default><Function>ClassName()</Function></Literal><Literal Editable="true"><ID>description</ID><ToolTip>描述</ToolTip><Default>該依賴屬性的描述</Default><Function></Function></Literal></Declarations><Code Language="csharp"><![CDATA[ #region $property$ (DependencyProperty)/// <summary> /// $description$ /// </summary> 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($defaultvalue$, new PropertyChangedCallback(On$property$Changed)));private static void On$property$Changed(DependencyObject sender, DependencyPropertyChangedEventArgs args) { // 獲取自身的引用$ownerclass$ source = ($ownerclass$)sender;// 處理邏輯$type$ newValue = ($type$)args.NewValue; }#endregion $end$]]></Code></Snippet></CodeSnippet> </CodeSnippets>??在 VS 中,自定義一個類 MyClass,該類繼承自 DependencyObject,在頁面中敲如 sldpc,彈出智能提示:
? 敲一下 Tab 鍵,則自動補全代碼:
??? 繼續點擊 Tab 鍵,可以在關鍵字之間切換。
?
節點說明:
<Title>表示此代碼段標題
<Shortcut>設置快捷鍵
<Description>對代碼段的描述
<SnippetTypes>可以包含多個<SnippetType>其取值有三種Expansion、SurroundsWith、Refactoring 。Expansion允許代碼插入在光標處;SurroundsWith允許代碼圍繞在選中代碼兩邊;Refactoring指定了C#重構過程中所使用的Snippet,在自定義Snippet中不能使用。如果該值不做設置,則Snippet可以放在任何地方。
<Snippet>節點是實現代碼管理的地方,其包含四個子節點<Code><Declarations><Imports><References>
1.<Code>
包含<![CDATA[]]>中,放置模版代碼,此節點設置Language(C# VB XML),Kind(類型:如方法體,方法聲明),Delimiter(分隔符,默認值是$)
?
2.<Declarations>
包含多個<Literal>和<Object>節點,<Literal>用于指定文本值<Object>用于聲明模版對象。筆者自理解為一個函數。以便code調用.
3.<Imports>
引入命名空間,只支持vb .? - -#.
4.<References>
添加程序集引用,只支持vb .? - -#.
?
?
函數只適合于C# 總共3個函數
1.GenerateSwitchCases(EnumerationLiteral),根據枚舉生成switch代碼.
2.ClassName() 返回類名:
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"><CodeSnippet Format="1.0.0"><Header></Header><Snippet><Declarations><Literal Editable="false"><ID>classname</ID><Function>ClassName()</Function><Default>ClassNamePlaceholder</Default></Literal></Declarations><Code Language="csharp"><!--[CDATA[ public class $EventHandlerType$ {//to do ....}]]--></Code></Snippet></CodeSnippet></CodeSnippets>3.SimpleTypeName(TypeName),在Snippet所在的上下文中推斷出TypeName參數的最簡單形式:
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"><CodeSnippet Format="1.0.0"><Header></Header><Snippet><Declarations><Literal Editable="false"><ID>EventHandlerType</ID><Function>SimpleTypeName(global::System.EventHandler)</Function></Literal></Declarations><Code Language="csharp"><!--[CDATA[ public class $EventHandlerType$ {// to do ... }}]]--></Code></Snippet></CodeSnippet></CodeSnippets>
3、擴展閱讀
??? http://blog.nerdplusart.com/archives/silverlight-code-snippets
轉載于:https://www.cnblogs.com/hebeiDGL/archive/2013/06/14/3136003.html
總結
以上是生活随笔為你收集整理的Visual Studio 的代码片段(Code Snippet)功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Transact-SQL 示例 - 如何
- 下一篇: 【转】visual studio 201