[UWP]为附加属性和依赖属性自定义代码段(兼容UWP和WPF) (2)

其中classname不可编辑,它使用了ClassName()这个代码段函数,返回包含已插入代码段的类的名称。其它可用的代码段函数可以参考这个页面:代码段函数 。

引用变量的语法是$变量名$,如下所示:

public static readonly DependencyProperty $MyProperty$Property = DependencyProperty.Register(nameof($MyProperty$), typeof($PropertyType$), typeof($classname$), new PropertyMetadata(default($PropertyType$), On$MyProperty$Changed)); 3.3 导入代码段

在菜单上选择“工具->代码片段管理器”:

[UWP]为附加属性和依赖属性自定义代码段(兼容UWP和WPF)

在“代码片段管理器”窗口中点击“导入”,选中需要导入的文件后打开“导入代码片段”,选择位置后点击“完成”即可完成代码段导入:

[UWP]为附加属性和依赖属性自定义代码段(兼容UWP和WPF)

3.4 最终成果

依赖属性的代码段:

<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Keywords> <Keyword>dp</Keyword> </Keywords> <SnippetTypes> <SnippetType>SurroundsWith</SnippetType> </SnippetTypes> <Title>Dependency Property</Title> <Author>dino.c</Author> <Description>For Dependency Property</Description> <HelpUrl> </HelpUrl> <Shortcut>dp</Shortcut> </Header> <Snippet> <References> <Reference> <Assembly> </Assembly> </Reference> </References> <Declarations> <Literal Editable="true"> <ID>PropertyType</ID> <ToolTip>属性类型</ToolTip> <Default>int</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>MyProperty</ID> <ToolTip>属性名</ToolTip> <Default>MyProperty</Default> <Function> </Function> </Literal> <Literal Editable="false"> <ID>classname</ID> <ToolTip>类名</ToolTip> <Function>ClassName()</Function> <Default>ClassNamePlaceholder</Default> </Literal> </Declarations> <Code Language="csharp" Kind="method body"> <![CDATA[ /// <summary> /// 获取或设置$MyProperty$的值 /// </summary> public $PropertyType$ $MyProperty$ { get => ($PropertyType$)GetValue($MyProperty$Property); set => SetValue($MyProperty$Property, value); } /// <summary> /// 标识 $MyProperty$ 依赖属性。 /// </summary> public static readonly DependencyProperty $MyProperty$Property = DependencyProperty.Register(nameof($MyProperty$), typeof($PropertyType$), typeof($classname$), new PropertyMetadata(default($PropertyType$), On$MyProperty$Changed)); private static void On$MyProperty$Changed(DependencyObject obj,DependencyPropertyChangedEventArgs args) { var oldValue = ($PropertyType$)args.OldValue; var newValue = ($PropertyType$)args.NewValue; if (oldValue == newValue) return; var target= obj as $classname$; target?.On$MyProperty$Changed(oldValue, newValue); } /// <summary> /// $MyProperty$ 属性更改时调用此方法。 /// </summary> /// <param>$MyProperty$ 属性的旧值。</param> /// <param>$MyProperty$ 属性的新值。</param> protected virtual void On$MyProperty$Changed($PropertyType$ oldValue,$PropertyType$ newValue) { }]]> </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> <Keywords> <Keyword>ap</Keyword> </Keywords> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> <Title>Attached Property</Title> <Author>dino.c</Author> <Description>For Attached Property</Description> <HelpUrl> </HelpUrl> <Shortcut>ap</Shortcut> </Header> <Snippet> <References> <Reference> <Assembly> </Assembly> </Reference> </References> <Declarations> <Literal Editable="true"> <ID>PropertyType</ID> <ToolTip>属性类型</ToolTip> <Default>int</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>MyProperty</ID> <ToolTip>属性名</ToolTip> <Default>MyProperty</Default> <Function> </Function> </Literal> <Literal Editable="false"> <ID>classname</ID> <ToolTip>类名</ToolTip> <Function>ClassName()</Function> <Default>ClassNamePlaceholder</Default> </Literal> </Declarations> <Code Language="csharp"> <![CDATA[ /// <summary> /// 从指定元素获取 $MyProperty$ 依赖项属性的值。 /// </summary> /// <param>从中读取属性值的元素。</param> /// <returns>从属性存储获取的属性值。</returns> public static $PropertyType$ Get$MyProperty$(DependencyObject obj) => ($PropertyType$)obj.GetValue($MyProperty$Property); /// <summary> /// 将 $MyProperty$ 依赖项属性的值设置为指定元素。 /// </summary> /// <param>对其设置属性值的元素。</param> /// <param>要设置的值。</param> public static void Set$MyProperty$(DependencyObject obj, $PropertyType$ value) => obj.SetValue($MyProperty$Property, value); /// <summary> /// 标识 $MyProperty$ 依赖项属性。 /// </summary> public static readonly DependencyProperty $MyProperty$Property = DependencyProperty.RegisterAttached("$MyProperty$", typeof($PropertyType$), typeof($classname$), new PropertyMetadata(default($PropertyType$), On$MyProperty$Changed)); private static void On$MyProperty$Changed(DependencyObject obj, DependencyPropertyChangedEventArgs args) { var oldValue = ($PropertyType$)args.OldValue; var newValue = ($PropertyType$)args.NewValue; if (oldValue == newValue) return; var target = obj as $classname$; } ]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets> 4. 结语

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpxgyy.html