今天在寫東西的時候,發現常用的代碼段裡沒有RoutedEvent的,因此,寫了一個代碼段,方便以後使用,順便記錄一下,如何做代碼段。
1、在項目中新建一個XML文件,將擴展名修改為snippet。
2、打開文件,然後右鍵--插入代碼段--snippet


3、出現默認的代碼段實例,修改不同的位置

4、獲取代碼段的目錄
代碼段目錄的取得方式是:工具--代碼片段管理器--位置(選擇語言和列表內容,位置會變)


5、將自定義的代碼段文件放到目錄中
6、調用代碼段

7、RoutedEvent代碼段
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>路由事件代碼段</Title>
<Author>衆尋</Author>
<Shortcut>roue</Shortcut>
<Description>生成默認的路由事件</Description>
<SnippetTypes>
<SnippetType>SurroundsWith</SnippetType>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>屬性類型</ToolTip>
<Default>EventHandler</Default>
</Literal>
<Literal>
<ID>RoutedEventName</ID>
<ToolTip>屬性名</ToolTip>
<Default>MyEvent</Default>
</Literal>
</Declarations>
<Code Language="XML">
<![CDATA[
public event $type$ $RoutedEventName$
{
add{ AddHandler($RoutedEventName$Event, value); }
remove{ RemoveHandler($RoutedEventName$Event, value); }
}
public static readonly RoutedEvent $RoutedEventName$Event=EventManager.RegisterRoutedEvent(
"$RoutedEventName$",RoutingStrategy.Bubble,typeof($type$),typeof(myclass));
]]>
</Code>
</Snippet>
</CodeSnippet>