代码片段
原创2023年12月7日大约 3 分钟
VS提供了代码片段减少开发工作量。
自定义代码片段
新建xml文件
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>comment</Title> <Shortcut>neverland</Shortcut> <Description>功能注释</Description> <Author>Neverland</Author> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal> <ID>Author</ID> <ToolTip>作者</ToolTip> <Default>Neverland</Default> </Literal> <Literal> <ID>Function</ID> <ToolTip>功能模块</ToolTip> <Default></Default> </Literal> <Literal> <ID>Description</ID> <ToolTip>方法描述</ToolTip> <Default></Default> </Literal> </Declarations> <Code Language="csharp"> <![CDATA[ /// **************************************************************************** /// @Author:$Author$ /// @Function:$end$ /// @Description: /// /// /// **************************************************************************** ]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
另存为
.snippet
后缀文件打开VS\工具\代码片段管理器
选择
MyCodeSnippets
导入自定义代码片段
输入Shortcut
里的内容,按两次Tab
键即可
依赖属性代码片段
<?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>
<Literal>
<ID>defaultvalue</ID>
<ToolTip>The default value for this property.</ToolTip>
<Default>0</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
public $type$ $property$
{
get { return ($type$)GetValue($property$Property); }
set { SetValue($property$Property, value); }
}
// Using a DependencyProperty as the backing store for $property$. This enables animation, styling, binding, etc...
public static readonly DependencyProperty $property$Property =
DependencyProperty.Register("$property$", typeof($type$), typeof($ownerclass$), new PropertyMetadata($defaultvalue$));
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
只读依赖代码片段
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propaonly</Title>
<Shortcut>propaonly</Shortcut>
<Description>只读附加依赖属性</Description>
<Author>Neverland</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>ownerclass</ID>
<ToolTip>拥有该属性的类</ToolTip>
<Default>ownerclass</Default>
</Literal>
<Literal>
<ID>defaultvalue</ID>
<ToolTip>该属性的默认值</ToolTip>
<Default>0</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
public static $type$ Get$property$(DependencyObject obj)
{
return ($type$)obj.GetValue($property$Property);
}
internal static void Set$property$(DependencyObject obj, $type$ value)
{
obj.SetValue($property$PropertyKey, value);
}
// Using a readonly DependencyPropertyKey as the backing store for $property$PropertyKey. This enables animation, styling, binding, etc...
internal static readonly DependencyPropertyKey $property$PropertyKey =
DependencyProperty.RegisterAttachedReadOnly("$property$", typeof($type$), typeof($ownerclass$), new PropertyMetadata($defaultvalue$));
public static readonly DependencyProperty $property$Property = $property$PropertyKey.DependencyProperty;
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
附加事件代码片段
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>eventa</Title>
<Shortcut>eventa</Shortcut>
<Description>附加事件</Description>
<Author>Neverland</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>eventName</ID>
<ToolTip>事件名称</ToolTip>
<Default>MyEvent</Default>
</Literal>
<Literal>
<ID>ownerclass</ID>
<ToolTip>拥有该属性的类</ToolTip>
<Default>ownerclass</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
public static readonly RoutedEvent $eventName$Event = EventManager.RegisterRoutedEvent("$eventName$", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof($ownerclass$));
public static void Add$eventName$Handler(DependencyObject dependencyObject,RoutedEventHandler handler)
{
if (dependencyObject is not UIElement uiElement) return;
uiElement.AddHandler($eventName$Event, handler);
}
public static void Remove$eventName$Handler(DependencyObject dependencyObject,RoutedEventHandler handler)
{
if (dependencyObject is not UIElement uiElement) return;
uiElement.RemoveHandler($eventName$Event, handler);
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
路由事件代码片段
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>eventdp</Title>
<Shortcut>eventdp</Shortcut>
<Description>路由事件</Description>
<Author>Neverland</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>eventName</ID>
<ToolTip>事件名称</ToolTip>
<Default>MyEvent</Default>
</Literal>
<Literal>
<ID>ownerclass</ID>
<ToolTip>拥有该属性的类</ToolTip>
<Default>ownerclass</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
public static readonly RoutedEvent $eventName$Event = EventManager.RegisterRoutedEvent("$eventName$", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof($ownerclass$));
public event RoutedEventHandler $eventName$EventClick
{
add { AddHandler($eventName$Event, value); }
remove { RemoveHandler($eventName$Event, value); }
}
void Raise$eventName$RoutedEvent()
{
RoutedEventArgs routedEventArgs = new($eventName$Event, this);
RaiseEvent(routedEventArgs);
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>